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

I have the following that works fine..

elseif( get_post_type($post->ID) == 'press' ){the_title_attribute();}

However, i need the aforementioned title to be wrapped with a h2 tag.

Tried this but it just puts the h2 after the title...

elseif( get_post_type($post->ID) == 'press' ){echo'<h2>'. the_title_attribute() .'</h2>';}

Any ideas?

Many thanks for any help :)

share|improve this question
Out of curiosity: why are you using the_title_attribute() rather than the_title()? – Chip Bennett Apr 13 '11 at 11:08
To clarify my comment above: the_title_attribute() is intended for outputting the Title specifically formatted for use in the title attribute of an anchor tag; on the other hand, the_title() is intended for general output of the Title. For your use case, you could specifically use get_the_title(), which returns, rather than echoes, the string. – Chip Bennett Apr 13 '11 at 12:37

The the_title_attribute() template tag is intended specifically for outputting the Title formatted for use in an anchor tag title attribute. The the_title() and get_the_title() template tags are intended for general print/display of the Title.

I would recommend using get_the_title(), rather than the_title_attribute(). To wit:

elseif( get_post_type($post->ID) == 'press' ){echo'<h2>'. get_the_title() .'</h2>';}

I believe this would be more semantically correct.

share|improve this answer
Brilliant guys, thanks so much for you help :) – user4630 Apr 13 '11 at 14:07
the_title has a param to set echo to false, like with the_title_attribute... – t31os Apr 13 '11 at 15:00
...certainly, but that's also what get_the_title() is designed for. :) – Chip Bennett Apr 13 '11 at 15:21

The the_title_attribute function already echoes, so you can't echo it's value, try this instead.

elseif( get_post_type($post->ID) == 'press' ){ ?><h2><?php the_title_attribute(); ?></h2><?php }

Or alternatively..

elseif( get_post_type($post->ID) == 'press' ){ echo '<h2>'; the_title_attribute(); echo'</h2>'; }

Or alternately:

elseif( get_post_type($post->ID) == 'press' ){ echo '<h2>' . the_title_attribute( '', '', false ) . '</h2>'; }
share|improve this answer
Or you could set echo to false for the_title_attribute(), like such: the_title_attribute( '', '', false ), which would allow for echoing the entire, concatenated construct. – Chip Bennett Apr 13 '11 at 11:10
I didn't check the docs... but yes, you could also do that.. :) I wrongly assumed it didn't have support for it(without checking).. – t31os Apr 13 '11 at 12:19
Thanks for all your help :) – user4630 Apr 19 '11 at 12:07

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.