I’m trying to find some logic that will echo a class only if the current page title matches the title in the li. I’m having trouble coming up with an ‘if’ statement that won’t apply to all the li in this query.
This is in wordpress.
Here’s the context:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" <?php if(NEED LOGIC HERE) echo 'class="current"'; ?>><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); endif; ?>
Any help would be much appreciated!
If you know that you are on a
single
of some sort (ie. a page, post or similar), then I suppose you could save the ID of that before your custom loop and compare to that:…sorry for changing the code style completely, I couldn’t make it readable using the template tags.
Oh, and of course you could compare the titles instead if that is actually more desirable (though obviously less precise), just use
get_the_title()
or$post->post_title
instead of$post-ID
.Ok, I figured it out. I needed to go outside the loop to create a valid if statement that read the link in the query AND the current page. Notice the $titleID.