Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<h2><span>
     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
       <?php the_title(); ?>
     </a>
</span></h2>
<?php 
   if ( has_post_thumbnail() ) {  ?>
      the_post_thumbnail('serivce-highlights'); 
   } 

the_content(' '); 
 ?>
</div>

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?

Thanks!

share|improve this question

1 Answer 1

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

 
discard

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.