HowTo: Hide the node title on a page
Last modified: September 21, 2007 - 04:59
You can hide node titles by specific node type by adding some code to your template.php file.
Here's the snippet, in this example it is ignoring the title on any node that is of the type "page" or "story". Note that we're also saving the title to another variable for use in breadcrumbs if desired.
<?php
//titles are now ignored by specific node type when they are anomalous in the design
$vars['breadcrumb_title'] = $vars['title'];
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if (in_array($node->type, array('page', 'story'))) {
$vars['title'] = '';
}
}
?>Here's an example with the code where it belongs in the _phptemplate_variables() function
<?php
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'page':
//titles are now ignored by specific node type when they are anomalous in the design
$vars['breadcrumb_title'] = $vars['title'];
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if (in_array($node->type, array('page', 'story'))) {
$vars['title'] = '';
}
}
break;
}
return $vars;
}
?>
Hidding by user
I have implemented a tip to allow the user type "__The title of node__" (double underscore) to hide the node title:
<?php
/**
* Override or insert PHPTemplate variables into the templates.
*/
function _phptemplate_variables($hook, $vars) {
switch ($hook) {
case 'page':
// add from here
if (preg_match('/^__(.*)__$/', $vars['title'], $regs)) {
$vars['title'] = '';
$vars['head_title'] = $regs[1];
}
// add to here
break;
}
return $vars;
}
?>
Note title appears on HTML head-title node...