WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'; } ?>">

     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
       <?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

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.