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:

  1. <?php if ($title): ?>
  2. <h1 class="page-title"><?php print $title ?></h1>
  3. <?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:

  1. /**
  2.  * Hook_preprocess_page().
  3.  */
  4. function theme_name_preprocess_page(&$vars) {
  5.  
  6. }

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

  1. //Remove title variable from certain Panel pages
  2. switch (true) {
  3. case arg(0) == 'page' && arg(1) == 'foo':
  4. case arg(0) == 'page' && arg(1) == 'fi':
  5. case arg(0) == 'section' && arg(1) == 'zoo':
  6. case arg(0) == 'bart':
  7. unset($vars['title']);
  8. break;
  9. }

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:

  1. <?php if ($title): ?>
  2. <h1 class="page-title"><?php print $title ?></h1>
  3. <?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.

Comments

Quite neat approach. Thanks for sharing, it worked out perfectly.

Since I´m using Panels to override the node display I was wondering how to apply your approach by node types. The title of the node appears dupplicate when is placed within a pane, which is really annoying. I tried out to adapt your solution in my case, without success (I´m a very begginer in PHP):


function theme_preprocess_page(&$vars){
switch (true) {
case arg(0) == 'blog' && arg(1) == ($node->type = 'blog'):
unset($vars['title']);
break;
}
}

Any suggestions?

Blair Wadman's picture

I think the case is wrong, in particular this bit: arg(1) == ($node->type = 'blog'):

Try this instead:


function theme_preprocess_page(&$vars){
switch (true) {
case arg(0) == 'blog' && $node->type = 'blog':
unset($vars['title']);
break;
}
}

This will check if arg(0) is equal to blog AND the node type is blog.

Thank you for a great article. I am a beginner in Drupal and wana ask you a question.

I have a view which displays fields from a 'person' type node.

URL is like mydomain.com/allteachers/ - this page displays all teachers (person node type) with:

- fullname (content field)
- short bio (content field)

I want mydomain.com/allteachers/ -> clicking on the title of a teacher should go to a Panel Page?

I don't want the Panel Page to go to mydomain.com/allteachers/fullname. I want the Panel Page to go to:

- mydomain.com/fullname (fullname is the nameofteacher).

In my view I want to link the title to a PANEL PAGE. ( I know I can link the title field to its node but I don't want that)

Thank you.

Thanks so much for posting this. Just what was needed... wasn't finding the solution anywhere else.

- XC

for me doesn't work, taking a deep look at the code i see that later the title will be setted in template_process_page() in template.php

Add new comment