1

I want to hide page title on a specific path, but I want to retain the page title between "title" tags in the header. I want the title to be exist in title bar of the browser, but that should not be displayed in the page for specific paths.

Is that could be done using theme_preprocess_page Or any other specific hooks out there ?

Is there any module?

What else could be done to prevent page title from displaying on the page in specific paths?

2
  • Assuming the title is being output in page.tpl.php then yes, a page preprocess hook is the correct place to make the alteration.
    – Clive
    Feb 7, 2015 at 11:22
  • @Clive, I have implemented theme_preprocess_page. However which element in the $variables should be changed to hide title on the page, but yet make it available in the title bar of the browser? Feb 7, 2015 at 17:17

3 Answers 3

2

There are a number of ways to do this, but this answer focuses on the considerations for handling things programmatically in custom code.

The <title> variable (for the browser title bar) is calculated as part of the "html" theme template, while the <h1> title variable (printed at the top of the page) is calculated as part of the "page" theme template. So in this regard if you implement some preprocess logic on the "page" output, you can remove the title from the page display while still keeping it intact for the browser bar. This is of course in contrast to more heavy-handed approaches, like calling drupal_set_title(), which impact static variables that may apply on a more global scope.

The trick to this is that, in most cases, your "page" template title is not actually initiated as a theme variable until template_process_page(), which runs after any preprocess functions. So you may not see a "title" variable within the scope of your own theme_preprocess_page(). The appropriate snippet from template_process_page(), where the title is added, is just:

if (!isset($variables['title'])) {
  $variables['title'] = drupal_get_title();
}

Given this background, you could manipulate the title variable direct by either:

  1. Setting $variables['title'] to an empty string within your theme_preprocess_page() (based on whatever conditions your desire). This way it will be set and therefore won't get filled-in via the template_process_page() code that runs later, but it will still be empty when page.tpl.php template renders things.
  2. Implementing a theme_process_page() function, which will run after template_process_page(), that unsets/nullifies the title variable set there.

You could also avoid touching the title variable itself altogether by just setting a boolean flag, like $show_title, in a (pre)process function. Then you would need to modify your page.tpl.php template with a very basic boolean check of $show_title that only prints the title <h1> if TRUE. That method is discussed in this post.

With any of these options you isolate your primary conditional logic, which can be as detailed as you want (should you discover new title visibility needs later), within the theme preprocessing layer. This will help keep things more maintainable for the long run.

2
  • Thanks for the answer. I am able to do it using template_process_page(). I just unset the $variables['title'] in my modules process_page and it worked. The title was removed from the page, but title was available in title bar, that is exactly what I wanted. Feb 8, 2015 at 2:45
  • Yeah, using theme_process_page() is probably even better. I tweaked the answer to include that option for reference.
    – rjacobs
    Feb 8, 2015 at 19:02
1

I recommend checking out the "Exclude Node Title" module. https://www.drupal.org/project/exclude_node_title

I think it can do exactly what you're asking. -Wrench

0

I suggest you another way, in your page.tpl.php do something like this

<h2 class="node_title"><span class="node-page-title">
<?php 
if(current_path()!='yourpath'){ // or any condtion you want
 print $title; 
}
?>
</span></div></h2>
3
  • Though this would work it adds at least one function call (and possibly more depending on the extent of the conditional logic needed) directly within the template, which would ideally be avoided. Still, for very simple needs it might be acceptable as a very quick solution.
    – rjacobs
    Feb 7, 2015 at 19:44
  • @rjacobs your told specific path its mean you should have a condition( maybe you specific paths (like specified type)),also you can check it in you template_process_page() and then set a variable (as example $var['myspecifed']=true;) and check it in your page.tpl.php file
    – Yuseferi
    Feb 8, 2015 at 7:35
  • 1
    sorry, my comment was probably not well-phrased. A conditional check, like your example, is obviously required. The point I was wanting to make is that any conditional logic that must call other functions is probably better placed within preprocess functions as opposed to the template directly.
    – rjacobs
    Feb 8, 2015 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.