
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.

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
Very nice little trick!
This is a great little trick. I am thinking of reaons to use it it now.
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.
Your Tips are small and very useful…Thanks…
Oh! So simple and yet so brilliant. Thanks for sharing.
Nice Tip, very useful thanks for sharing!
Nice, short and handy little tip as always. Thanks for sharing!
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/
Does not All in SEO pack does this itself ??
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 !
Very useful tips. :B
Quiet useful and nice trick. Thanks for sharing.
Trackbacks: