Styling of WordPress post titles

Since the beginning of time (well, for a long time), I’ve always wanted to do something like this with my WordPress post titles:

of

You see, the italicised of. Or you may want to change the typeface, the colour, or style in some other way. I posted about this on Twitter and, as usual, about four million kind souls replied with their solutions.

So, how is it done? I chose to go with the brilliantly powerful custom fields. I also use these to display different header images for ILT, and alternative background images and colours on this site.

Step one: on your edit post page, just create a new custom field like so:

custom fields in wordpress

Then open your single.php in your themes [wp-content/themes/your_theme] folder, and find where it outputs the title of the post, and replace with something like:


<h2><a href="<?php the_permalink() ?>" title="<?php the_title_attribute() ?>">
<?php
$altitle = (get_post_meta($post->ID, "custom_title", true));
if($altitle !="") echo $altitle;
else the_title_attribute() ?>

</a></h2>

Perhaps this could be improved. Anyone? This solution works for me because it enables me to style post titles per post.

If you’d like a plugin solution, then check out Kari’s Title Style from the WordPress plugin directory. I’m using it on this site.

________________
Special thanks to @essenmitsosse and the myriad others who replied to my original tweet.

Afterthought: I wonder how you could get my above hack to work in Thematic?

This entry was posted in computer stuff and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

12 Comments

  1. Posted August 13, 2009 at 8:47 am | Permalink

    My boss Phyllis Zimbler Miller (@ZimblerMiller) figured out that you can put a <em> in title field — no extra code necessary. Try just using <em></em> in the title field — see what happens.

  2. Posted August 13, 2009 at 8:55 am | Permalink

    Hadn’t thought of that. I wonder how SEO-friendly that is?

  3. Posted August 13, 2009 at 8:58 am | Permalink

    Annoying that it isn’t possible to directly write the html. A necessary evil I guess, but again good work from Kari

  4. Posted August 13, 2009 at 9:00 am | Permalink

    Esben:
    I’m sure Kari could get that plugin to fry eggs and make coffee. Just give him a few minutes :)

  5. Posted August 13, 2009 at 9:29 am | Permalink

    The problem why this naturally don’t work is, that html doesn’t work for the title in the head. You would need one title with and one without html. Or at least I guess thats the original reason …

  6. Posted August 13, 2009 at 9:58 am | Permalink

    I have no clue if there’s just something different with my install, but I’ve never had problems with HTML in the title. It’ll show the escaped title in the wp-admin, but the actual post will show the styled content. I can’t find anything in the settings.

    When you tried, would it just show “STYLING <em>of</em> WORDPRESS?”

    Maybe it has something to do with my database? Maybe I forgot to escape things?

  7. Kari Pätilä
    Posted August 13, 2009 at 10:15 am | Permalink

    @Yael K. Miller
    Hadn’t taken that into account, and already had to push one bug fix.

    @Esben Thomsen
    How would you like to be able to input the HTML?

  8. Posted August 13, 2009 at 10:36 am | Permalink

    I know I have seen a plugin somewhere that makes the post titles editable with a WYSIWYG…

    I googled it and looked on the Extend db, to no avail. This was the closest I found

  9. Posted August 13, 2009 at 12:08 pm | Permalink

    It might be smarter (more future-compatible, and more semantic) to put the word “of” in span tags instead; something like:

    <span class="special">of</span>

    I also have used em tags in the title field without problem, though usually for actual emphasis or titles of works.

  10. Posted August 13, 2009 at 12:08 pm | Permalink

    I tried to argue for this ages back and was rebutted:
    http://core.trac.wordpress.org/ticket/1371

    I still think Otto42 was fundamentally wrong, in that he concluded that any and all HTML in titles would be purely presentation, while what I was trying to assert, and continue to, is that the likes of em and code are actually semantic in nature and should be allowed in titles. But I didn’t have time to debate with a developer who wasn’t open to reason and obviously wasn’t going to change his mind, so I gave up.

    The fact that plugins have sprung up shows there’s still a desire for it, but I don’t think it’ll ever gain official integration into the WordPress core. Ah well!

  11. Kari Pätilä
    Posted August 13, 2009 at 12:13 pm | Permalink

    The reasoning behind the plugin was that I didn’t want to touch the original titles, just alter them on the fly. Otherwise you might run into problems.

  12. Posted August 30, 2009 at 1:03 am | Permalink

    I just tried it today and noticed, that it’s really working to put in the normal title. I don’t know why I thought, it wouldn’t work.

One Trackback

  1. […] this page was mentioned by David McKendrick (@davidandgoliath), Grameen Solutions (@grameensol), Asif Anwar (@asifanwar4seo), Andrew (@bacbay), Michael Davis (@web2feed) and others. […]