Take the 2-minute tour ×

So I've got a list of posts on the right side of a page. I'm trying to make it so that when you're looking at one of the posts the corresponding link (on the right side of the page) is selected (just like when you're on a page, and that menu item in the nav is highlighted). Make sense?

I'm trying to use this "if" statement to add the class "selected-highlight"

<div class="service-highlight-box-sidebar <?php if(single_post_title() == the_title()){ echo ' selected-highlight'; } ?>">

       <?php the_title(); ?>
   if ( has_post_thumbnail() ) {  ?>

the_content(' '); 

When I load the page it adds <div class="service-highlight-box-sidebar post_title the_title selected-highlight"> to every one of the post links on the right of the screen. Note: it doesn't actually add "post_title" and "the_title", it'll add the dynamic post title and dynamic link title in their place. The problem is that it's adding them to the class instead of using them in the if statement to determine weather the class should be added.

Should I look for the error in the code snippit above, or could this be a bigger problem with my template, or just a "no no" with wordpress in general?


share|improve this question
add comment

1 Answer

This is because the_title() prints the title. You want get_the_title() to 'get' the title. Similarly single_post_title prints the title (by default). This can be prevented by using the second argument, e,g,: single_post_title('',false)

share|improve this answer
add comment

Your Answer


