0

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.

1 Answer 1

0

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

<li<?php echo get_queried_object_id() == get_the_ID() ? ' class="active"' : '';?>>
1
  • Thank you! That's the exact and simple answer I was looking for. Commented Mar 29, 2022 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.