Blair Wadman 4 minute read

Remove Page Titles from Panels & Views

There may be times when you want to remove a page title from a page, but leave it as the meta title. By default, Drupal will display the node title in the h1 tag. If you take a look at page.tpl.php, you will see it there. In most themes, it will look something like this:

<?php if ($title): ?>
   <h1 class="page-title"><?php print $title ?></h1>
<?php endif; ?>

Now you may be asking why on earth would I want to remove the main page title? I will give you an example. The Panels module gives you the ability to set a title for a panel page. This title will appear in the main h1 on the page and also in the meta title tag (across the top of the browser). Because Panels brings in content from other places such as views, blocks or nodes, you may not actually want this title on the page, as each mini panel may have its own title. Sure, you can just leave the title field blank, or set it to no title. However, what if you want to keep the title for the meta title tag, but not have that title displayed in the main h1? Panels doesn't give you the ability to do that out of the box.

There is a module for that!

It is true, there is a module that handles this situation. It is called nodewords pagetitle.

However, there are two problems with this module:

1) You have to also have nodewords beta9 enabled. It will not work with the current stable version of nodewords. Upgrading to beta9 involves upgrading to beta5 first, running update.php and then upgrading to beta9 and running update.php again. 2) You have to add the title in a seperate admin interface. If you are already using the page title module to handle seperate meta titles for nodes, this makes your site inconsistent from a usabilty point of view. For nodes, content creators can add a sepeate node title right there on the node edit form. But for Panel or View pages, they have to go to a seperate admin screen. A recipe for confusion.

So, my prefered approach, for Panels at least, is to set the title in the Panel page itself, which the Panel module allows. And then, removing the page title and associated h1 tag from the Panel page.

Removing the title options

To remove the title and the h1 tag, you have two main options: 1) In CSS, set display:none. I do not recommend this though. It is not good practice to set display:none to content that does exist in the html page. 2) Remove the title and h1 totally from the page. To do this, follow the steps below:

Removing the title and h1 solution

1) Make a list of the paths for the Panel pages 2) Go to the theme's template.php file 3) Find the pre process function for pages. If this doesn't exist, create on as such:

/**
 * Hook_preprocess_page().
 */
function theme_name_preprocess_page(&$vars) {

} 

4) Add the following switch statement to the preprocess function: In this example, I will assume the following is a list of URLs for the Panel pages where I want the title removed:

http://www.example.com/page/foo http://www.example.com/page/fi http://www.example.com/section/zoo http://www.example.com/bart

//Remove title variable from certain Panel pages
  switch (true) {
    case arg(0) == 'page' && arg(1) == 'foo':
    case arg(0) == 'page' && arg(1) == 'fi':
    case arg(0) == 'section' && arg(1) == 'zoo':
    case arg(0) == 'bart':
      unset($vars['title']);
      break;
  }

In the switch statement, if the case returns TRUE, then the code to unset the page title will be executed. Therefore, for those pages, the title will be removed totally from the page. Because the title variable is unset, the main h1 element will also not appear. That is assuming that it is wrapped in a condition that checks for the title variable. Here is a repeat of the code past at the beginning of this post, which shows that:

<?php if ($title): ?>
   <h1 class="page-title"><?php print $title ?></h1>
<?php endif; ?>

Therefore, because $title has been unset, the condition here will return false, and the h1 element will not be displayed. But it will remain as a meta title, which is exactly what we want.