Making WordPress page headings different from the menu items

By Tim Priebe on May 17, 2010

By Tim Priebe on May 17, 2010

Just a quick WordPress tip this time for theme developers, or people who just want to customize a WordPress theme they’ve purchased.

Sometimes there are reasons to make a menu item slightly different from the title of the actual page. Perhaps your menu space is limited, but you have plenty of room in the content area. A typical example is using “FAQ” in the menu, but then the page heading actually shows “Frequently Asked Questions.”

A great way to accomplish this is the Custom Fields in WordPress, combined with a bit of theme customization.

Normally, you would insert the title of a page like this:

<?php the_title(); ?>

Instead, you can create a custom field. We’ll call it “custompagetitle” for this example. On the FAQ page, you would enter ‘FAQ” as the title of the page, then a custom field called “custompagetitle” and enter “Frequently Asked Questions” there. Then, instead of the previously mentioned code, insert this code:

<?php

// See if there's a custom field custompagetitle,
// and if so, use it as the page's title

$realtitles = get_post_custom_values('custompagetitle');

if (is_array($realtitles)) {

$realtitle = implode($realtitles);

}

if ($realtitle)

echo $realtitle;

else

the_title();

?>

Not only does this code use your custom heading, but if there is no custom heading, it will default to the actual page title.

Ready to get started?

Ready to take your digital marketing to the next level? We're here to help. Let's talk.

2 Comments

  1. Avatar Sean Sanders on May 27, 2010 at 4:04 pm

    All that code in one line:

    <h2>ID,'alternatepagetitle' , true)) ? $title : get_the_title(); ?></h2>


  2. Tim Priebe Tim Priebe on October 13, 2010 at 9:52 am

    With WordPress 3.0, this is no longer necessary if your theme uses the new menu system. That lets you rename the page at the menu level instead of the other way around, like we described in this article.