admin管理员组

文章数量:1204602

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.

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.

Share Improve this question edited Mar 29, 2022 at 10:11 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Mar 28, 2022 at 18:11 Jorge MonteJorge Monte 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

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

本文标签: singleGive active class to current page on a while loop WP query for custom post types listing