Give active class to current page on a while loop WP query for custom post types listing

I have a single-programs.php page and at the top of this single page I need to list all the entries from the custom post type programs. Is there a way to give active class to this list for the current page (current single post type ?

Here is my code:

?php
        $args = array(
            'numberposts'   = -1,
            'post_type'     = 'programs',
            'order'         = 'ASC'
        );

        $the_query = new WP_Query( $args );

        ?
        ?php if( $the_query-have_posts() ): ?
        ul
            ?php while( $the_query-have_posts() ) : $the_query-the_post(); ?
                li
                    span class=background/span
                    h3 class=title?php the_title(); ?/h3
                    a href=?php the_permalink(); ?/a
                /li
            ?php endwhile; ?
        /ul
        ?php endif; ?

        ?php wp_reset_query();  // Restore global post data stomped by the_post(). ?

This code is generating a list with my 4 items related to programs, however I need to include a class active to the current single page.

Topic single custom-post-types Wordpress

Category Web


Assuming you want to add the class to the li element.

<li<?php echo get_queried_object_id() == get_the_ID() ? ' class="active"' : '';?>>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.