theme node titles

Hide the Node Title on a Page (6.x)

Last modified: August 27, 2009 - 00:22

Hide Node Title by Content Type

You can hide node titles by specific content type by adding some code to your template.php file.

This example ignores the title on any node that is of the type "page" or "story". Note that it also saves the title to another variable for use in other parts of the page.tpl.php if desired. Also note that these instructions cover the case where the function in question doesn't exist. If you get an error that says "cannot redeclare function ...", then you need to add the code to the existing function. Finally, you may have to go to admin/build/themes and save the themes form there to rebuild the theme registry before the changes take affect.

<?php
function THEMENAME_preprocess_page(&$vars) {

 
// Titles are ignored by content type when they are not desired in the design.
 
$vars['original_title'] = $vars['title'];
  if (!empty(
$vars['node']) && in_array($vars['node']->type, array('page', 'story'))) {
   
$vars['title'] = '';
  }

}
?>

HowTo: Replace node title with a related image

Last modified: September 1, 2009 - 07:28

I took over a static site, designed with Dreamweaver. The site design and layout were beautiful, and one of my goals for converting the site to Drupal was to maintain as much of the site's visual appeal as possible. One of the visual elements were sharp graphic titles on each page. And so began my quest to figure out how to replace Drupal's text title with these image versions. I tried several methods before coming across the excellent article above (this page's parent), which gave me the following idea (much of the code is lifted directly from that article).

Overview: basic concept

A node on your site has a title, like "Bob's Antiques & Junk", and you want to display the related image file "bobs_antiques_junk.gif" instead of this text title when displaying the node as a page. Furthermore, you want the original title as alt text on the image, and you want "graceful degradation", so the original text title is displayed if the "related image" file does not exist.
What this does is essentially make the Title field of the nodes into an special image selector - if you use a title with an associated image, the image will appear, otherwise, the text title will appear - sweet!

Prefixing Node Title with Icon

Last modified: August 26, 2009 - 22:46

Unfortunately the node title does not carry a handy CSS tag, and trying to select it by context is not very reliable either because it doesn't (eg Garland) have a parent <div> container that distinguishes it from other <h2> elements in page content (eg the comments header).

Hide the Node Title on a Page (5.x)

Last modified: August 27, 2009 - 00:23

Hide Node Title by Content Type

You can hide node titles by specific content type by adding some code to your template.php file.

This example ignores the title on any node that is of the type "page" or "story". Note that it also saves the title to another variable for use in breadcrumbs if desired.

<?php
// Titles are ignored by content type when they are not desired 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'] = '';
  }
}
?>

This is where the code belongs in the _phptemplate_variables() function:

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'page':
   
// Titles are ignored by content type when they are not desired 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;
}
?>

Hide Node Title on Front Page

Inserting HTML into node titles

Last modified: August 26, 2009 - 23:55

Drupal strips all HTML from node titles when they're displayed to prevent XSS exploits. However, there are some cases where inline HTML elements are needed, such as when titles contain scientific names.

To work around this, we will use pseudotags like [i] and [/i] which Drupal will safely translate into real HTML tags when the page is displayed.

The PHPTemplate file page.tpl.php will need to be edited in order to make this trick work. Specifically, we need to add two new functions.

<?php
function bb2html($text) {
 
$bbcode = array(
                 
"[strong]", "[/strong]",
                 
"[b]""[/b]",
                 
"[u]""[/u]",
                 
"[i]""[/i]",
                 
"[em]", "[/em]"
               
);
 
$htmlcode = array(
               
"<strong>", "</strong>",
               
"<strong>", "</strong>",
               
"<u>", "</u>",
               
"<em>", "</em>",
               
"<em>", "</em>"
             
);
  return
str_replace($bbcode, $htmlcode, $text);
}

function
bb_strip($text) {
 
$bbcode = array(
                 
"[strong]", "[/strong]",
                 
"[b]""[/b]",
                 
"[u]""[/u]",
                 
"[i]""[/i]",
                 
"[em]", "[/em]"
               
);
  return
str_replace($bbcode, '', $text);
}
?>

Syndicate content
 
 

Drupal is a registered trademark of Dries Buytaert.