Wednesday, September 28, 2011

Remove Page Titles from Panels - Drupal Theme

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.

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:

//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':  

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.

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.

Full article: