Post Pic

Allow title overwrite on your WordPress blog

The the_title(); function allow you to simply display the title of a post or page. Sometimes, you may need to be able to overwrite this function and use a custom title. here is how to do it, using a custom field.

Nothing hard with this recipe: Simply paste th following code on your single.php and page.php, where you want the post/page title to be displayed.

<?php $title = get_post_meta($post->ID, "custom-title", true);
if ($title != "") {
echo "<h1>".$title."</h1>";
} else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>

Once done, you just have to create a custom field named custom-title and type your custom title as a value.

21 Responses

Sep 10 2009 21:27

Very nice little trick!

Sep 11 2009 13:12

This is a great little trick. I am thinking of reaons to use it it now.

Sep 11 2009 14:49

One thing I like to do when checking for custom meta data is to actually perform a string count on it before letting it pass. I find my authors sometimes leave spaces in these custom fields, then complain that “there’s nothing there, why wont it go away!”.

A little strlen of say, 2 or 3, normally catches their oddities.

Sep 12 2009 15:02

Your Tips are small and very useful…Thanks… :)

Sep 13 2009 22:43

Oh! So simple and yet so brilliant. Thanks for sharing.

Sep 15 2009 00:53

Nice Tip, very useful thanks for sharing!

Sep 15 2009 21:53

Nice, short and handy little tip as always. Thanks for sharing!

Sep 21 2009 10:05

Humm… Really nice trick, I was thinking about a easy solution for change my Page titles because sometimes I want display one little word at menu (Contact) and a long title in page itself, just like (Give me a note right now…) and with this simple tip I can do it easier then never. \o/

Oct 01 2009 17:14

Does not All in SEO pack does this itself ??

Oct 21 2009 13:35

This tips is what I was looking for !

But I have a question : Is it possible to setup multiple custom titles ?
And if yes, how the code should be ?

Thanks,
Ton blog il déchire Jean-Baptiste ! :)

Oct 27 2009 18:49

Very useful tips. :B

Jan 01 2010 19:35

Quiet useful and nice trick. Thanks for sharing.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required