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 badges1 Answer
Reset to default 0Assuming 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
版权声明:本文标题:single - Give active class to current page on a while loop WP query for custom post types listing 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738655970a2105143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论