wordpress: how to force a page title using php

[posted: ]  [0 comments]

the problem: i have a special wordpress page (gallery) with custom theme (“page attributes”, “template”) which executes some of it’s own php code and database queries.
depending on the output of the php code, i’d like to add something to the <title> tag of that page, which is automatically created by the wordpress function get_header(). in other words, i’d like to force a custom page title through php.

i found the solution to this problem in the wordpress.org forums, where user nexusmedia had the same goal.
it is a hack, but doesn’t require editing of any wordpress core files. all you need to do concerns your theme’s php files: add one line to header.php, and the rest takes place in your special template (in my case: gallery.php).

header.php:
replace
<title> ... </title>
with the following code:
<title><? global $title; ?><? if ($title): ?><?=$title?> <?php echo " | "; bloginfo('name'); ?> <? else: ?><? wp_title('');?><? endif; ?></title>
 

gallery.php: (or whatever your special theme file is called)
replace
get_header();
with this:
global $title;
$title = 'My Custom Title';
get_header(); ?>

if you’re using the “all in one SEO” plugin, don’t forget to deactivate it on the concerned page(s) and place a checkmark at “Disable on this page/post”.

[category: general] [tags: , ]

Leave a Reply