Saturday, July 27, 2013

How to List Posts and Highlight Current in WordPress

You can list posts using get_posts() function , also the opening list tag above which replicates that functionality by seeing if the ID from the current query matches the ID from the current iteration of the loop.
<ul>

<?php
$lastposts = get_posts('numberposts=5&orderby=rand&cat=-52');
foreach($lastposts as $post) :
setup_postdata($post); ?>

<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="current"'; } else {} ?>>

<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

</li>

<?php endforeach; ?>

</ul>


Disqus Comments