!How to hide a node title?

4 Jul 2007
Question Status: 
Question details: 

So a client wants a site with a custom front page. After struggling with Panels and a number of options, I decided the easiest way to go about this particular request was a regular node, with blocks turned off.

One problem: the node's title was also appearing at top, messing up the graphic look needed. I wanted no title to appear, but Drupal doesn't allow a node without a title, and there's no built-in way to have a node appear with its title invisible.

Share/Save

Comments

Jake Strawn's picture

I like this solution for the "quick fix"...

However, it seems like there should be a much easier (Modular) solution to this. This is an option that I use VERY regularly when putting sites together... there may be tons of splash pages for sections of a site that are heavy on graphics, and nothing like the content of the rest of the site.

I'm new to drupal myself, but couldn't it be solved using page-front.tpl.php for your homepage, and removing the title from that one?

I'm still trying to figure the best way to display custom content using views, cck, the nodeteaser on your recommendation, and my custom regions that I have in my homepage layout.

seriously though, this should be a default option...a lil checkbox for "display title" checked by default... or a pretty simple module could do the trick. " I say that, but my first custom module has yet to be finished... so easy sounds easy, but maybe not quite...

Drupalace's picture

Hello! I believe you are completely right: a separate tpl.php page for the homepage is one way to go about it, and is a good solution for many custom page needs. I myself haven't implemented that, but I've seen the method suggested in answer to many drupal.org forum questions. All it takes is a little willingness to learn to parse PHP; for tpl.php files, I find that isn't too hard. (But I haven't tried any really fancy tricks yet, either.)

I also agree thoroughly about having show/hide title (or other fields) as a per-node checkbox option; that would be wonderful! To have titles, teasers, author info, etc. all implemented consistently with a simple option for show/hide, as a part of Drupal core, would add great flexibility to page display.

Shadlan's picture

It's not working in Drupal 5... Any other ideas?

Drupalace's picture

Sorry for the slow reply; I've been a-travelin'.

Let me try the hidden-node-title technique here again. Steps in my test:

1. I created a new page with title "Testing Hidden Node Title" (without quotes). 

2. Per instructions, I opened the theme's page.tpl.php file, found the line 

<?php if ($title!): ?>

 and changed that to

<?php if ($title!="Testing Hidden Node Title"): ?>

 3. I saved the theme's page.tpl.php file and refreshed the test page.

Hmm, seems to work fine here. The test page is at http://www.drupalace.com/page_testing_hidden_node_title ; it now appears without title, at least for me. (Note: the title still appears in the browser's window bar at top, which is fine by me.)

If it won't work for you, I can only suggest the problem is perhaps:

a. Syntax error in your page.tpl.php file modification; or

b. You're modifying page.tpl.php file in the wrong theme (please don't laugh; I've done that sort of goof so many times); or

c. For some reason, this technique doesn't work with whatever theme you're using. (Any theme I'd know? FYI, I'm using my own Zen-derived theme.)

Can you test again and confirm that it doesn't work? 

maccnl's picture

Thanks! Like the tip!

Unknown Drupaloid's picture

Thanks so much for this! So simple.

Josh McKenney's picture

The problem with this solution is that it will also set your page title to 'x' and you should always have a page title that is more descriptive than just your website name... I am running into this a lot:(

Drupalace's picture

It's a good concern - though let me note again that even using this technique, the true page title should still appear in the browser window's title bar. In the page source code too, it should still within the <title> tag, and thus be visible to search engines too.

If I'm not mistaken, all that should change is that display of the title within the page text is turned off, so there shouldn't be a problem. Or am I missing something?

Incidentally, there's a Page Title module which lets you change the default display of page titles in browser window title bars. It also provides for alternate page title display of any node. I gave it a try here, keeping this page's title as "How to hide a node title?", while inputting the alternate title "How to hide a node title? (Alternate title using Page Title module)"

But, nothing seems to change! Neither my displayed page, nor my browser title bar, nor the page source code, are showing any change to this page's title tag. (Let me know if you see differently.) 

Similarly, I tried providing an alternate page title to my hidden-title test page at

http://www.drupalace.com/page_testing_hidden_node_title .

But there too, no change. 

So, I'd like to report here on how the Page Title module affects what we can do with titles, but alas, the module doesn't seem to be working for me!  : (

Sam Hassell's picture

Hey, I'm familiar with the page_title module. Make sure you read the included README.txt file as you need to make some simple changes to your theme's template.php file for the module to work correctly.

Drupalace's picture

Doh! You are of course correct. I can think of all the times I've properly studied my modules before installing, following the instructions properly... and all the times I've just dumbly tossed one in, expecting it to work automagically.

In this case, there are indeed special instructions: this code needs to be added to the theme's template.php file:

function _phptemplate_variables($hook, $vars) {
  $vars = array();
  if ($hook == 'page') {

    // These are the only important lines
    if (module_exists('page_title')) {
      $vars['head_title'] = page_title_page_get_title();
    }
  }
  return $vars;
}

 Just pasting it in resulted in errors, as my Zen-based theme's template.php file already included

function _phptemplate_variables($hook, $vars = array())

  switch ($hook) {
    case 'page':

...followed by more code. 

Things worked when I added the "only important lines" of the Page Title code after the above code, resulting in

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':

    if (module_exists('page_title')) {
      $vars['head_title'] = page_title_page_get_title();
    }

 ...more code.

And with that, the alternate titles I mentioned are showing up in the browser headers! It works!

Moral: Read the Instructions. Thanks for the reminder! 

Jeff's picture

Drupal wraps the title in a div called #content-header

Just set #content-header to {display:none;} in the css and it works fine.

Drupalace's picture

Hey, I like that! It sounds ideal for removing title display from all pages - nice and simple.

Is there a CSS-based method for removing title display from a single page? Or is the page.tpl.php modification suggested on this page the better method for that?

Unknown Drupaloid's picture

Haven't tried this but if you want to change it for a single page, just overload the content header class by adding the following code to the page:

 

<STYLE>

#content-header

{display:none;}

</STYLE>

That will override the style sheet for just that id for just that page.  Only thing I'm not sure about is whether drupal allows you to do this properly - as I said, I haven't tried it in drupal.

 

Drupalace's picture

Right, the {display:none;} CSS tag for the title should do the job fine; the trick for Drupal, I think we agree, is how to get Drupal to apply it to one page only. The solutions I can think of include creating a block with the CSS code invisibly embedded within, and having that block display only for the target page. (The Side Content module might be able to do the job as well.)

Or perhaps the CSS code can be embedded directly within the node's Body field itself, or some other text field appearing on the page, properly headed with

<style type="text/css">

Let's try it - right here, using the code in this humble comment. I'll paste CSS into this comment (with rich text editor off and input format set to something that won't mess with the text), using the following simple code to turn all comment text on this page brown:

<style type="text/css">.comment p {color: brown; } </style>

If comments are brown, then there's one way to change the CSS for a single page: just bury the code within any text field appearing on that page.

EDIT: Removed code from this comment, as it served its purpose. It worked - though with a couple of surprises. First, the "brown" text it created looked awfully red to me; perhaps that's just a quirk of the word "brown" in CSS. Second, it changed the color of comments on ALL pages. That's a bit of a mystery to me... I would have thought it'd change the color only on a page displaying this comment. Any insights, anyone?

Pete Bacon Darwin's picture

I just used the following code in my page.tpl.php:

<?php if ($title && !$is_front): ?>
<?php echo $title; ?>
<?php endif; ?>

Drupalace's picture

Looks like another fine method. Thank you!

Unknown Drupaloid's picture

This is an adaptation of an earlier post, but I've found it useful...

I've modified page.tpl.php as identified above with the following:

                <?php if (substr($title,0,1)!="!"): ?><h1 class="title">
                <?php print $title; ?></h1><?php endif; ?>

Now, anytime I want a title to be hidden, just start it with an "!", almost kindof like a switch

Drupalace's picture

This is great as a simple switch that any contributor can easily understand. Thanks!

Jeff's picture

I've put this code in and it's not working for me.  Not matter what I do the title still appears at the top of my content page. 

I've cleared cache and done all the tricks listed on this thread.

 

Drupalace's picture

Hi Jeff. I gave the method a try on this page, to see whether I can confirm its success:

1) I found the page.tpl.php file in this site's theme. (I doubt you've made an error here, but for anyone else following: Be sure you're modifying the file for the correct theme. I make that sort of dumb mistake all the time.)

2) I found the code that outputs titles above nodes. Here's how it looked originally in my theme:

<h1 class="title"><?php print $title ?></h1>

Here's how it looks after I modified it, per the posted hint:

<?php if (substr($title,0,1)!="!"): ?>

<h1 class="title"><?php print $title ?></h1>

<?php endif; ?>

3) I saved the file, and edited this page, placing a "!" in front of the title.

4) Result: It appears that the title has disappeared as promised. I'm not seeing the title appear, in this or other browsers.

If your attempts still aren't working for you: Does your theme's code vary substantially from the above? If you post a clip here, maybe I or other readers can work out the problem. 

Finally, an off-topic message to any later reader: I updated a comment below related to CSS experiments performed on this page; I've undone the comment color changes mentioned there. I'll also later undo the missing title mentioned in this comment, resetting the page for possibly more experiments.

The point is: I'm using this poor page for all sorts of experiments, so depending on when you visit, a change mentioned in a comment may or may not still be in effect. FYI.

Jeff's picture

I found out why it wasn't working before.  I didn't place the "!" in front of the title. 

That now presents a new problem though.  Even though the title disappeared from the content page ... My <title> tag now has a "!" at the front.

Thank you for your help.

Drupalace's picture

Yeah, I get the "!" in front of the title tag too - and, of course, it'll appear in the node title in any lists. That'd be the unavoidable downside of this technique. (You could, of course, modify the technique to use any more preferable symbol - even a space? Also, if the concern is the "!" messing up the first word of the title for SEO purposes, I would think that beginning the title with "!" + space would resolve that.)

jerror's picture

Hey, I was trying to do the same thing, except the title in question was a node that was stickyied into the front page of content. It also had a background image which included the title.

 

I changed:

<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2><?php endif; ?>

to

<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print ($title!="!welcome") ?></a></h2><?php endif; ?>

in my node.tpl.php file

after making the above mentioned changes to my page.tpl.php file for future "want to hide that title" scenarios, cheers.

 

Also I came ot this after many failed attempts to install the auto node title module (probabally because i didnt read the release notes) :-X

jerror's picture

oops! got a littel carried away

you want it more like:

<code>

<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php if (substr($title,0,1)!="!")print $title; ?></a></h2><?php endif; ?>

</code>

i guess thats a more complete argument or whatever, the latter would just return "1" if it wasnt "!welcome" or "0"

 

jerror's picture

Hey, I was trying to do the same thing, except the title in question was a node that was stickyied into the front page of content. It also had a background image which included the title.

 

I changed:

<code>

<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2><?php endif; ?>

</code>

to

<code>

<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print ($title!="!welcome") ?></a></h2><?php endif; ?>

</code>

in my node.tpl.php file

after making the above mentioned changes to my page.tpl.php file for future "want to hide that title" scenarios, cheers.

 

Also I came ot this after many failed attempts to install the auto node title module (probabally because i didnt read the release notes) :-X

Ian Korman's picture

The coding methods didn't work for me. However, the proper way and the best way is not to mess with code in your core theme files. You should use the Drupal method of creating unique content types for now until they give us a clickable option in core not to show titles, published information, etc.

Here is how you do it (I happen to be using Garland theme):

1) Create a new content type. I called mine 'No Title Page'. The machine readable name is no_title_page.

2) Within your theme folder, copy the file called node.tpl.php to a new file, in my case node-no_title_page.tpl.php. Obviously, you need to use node-(new content type machine readable name).tpl.php

3) Now, you can just go into your new file and remove the lines that tell drupal to print the title.

This method results in no '!' in the name and it automatically shows you that this content is special when you go to edit content. If you use the coding methods discussed here then you will just create an ordinary page and this page isn't an ordinary type.

BTW, if you follow these steps then you could also create new content types with node files from different themes to easily create more individualized nodes.

 

Drupalace's picture

I agree: you really don't want to muck with code in core theme files. Or for that matter, code in modules, even contributed ones. It's not so much a matter of possibly breaking things (which is possible, of course, but not if you make safe modifications); the biggest annoyance IMO is that every custom mod means something another bit of work to re-do when updating core or modules. 

A unique content type, with title-printing code removed, is yet another good solution for removing titles; thanks for the idea! On the downside, I can imagine that a user who just wants to hide titles on a page or two won't be too enamored with the trouble of creating a new content type – and it all falls flat if, for whatever organizational purposes, the user wants the title-less nodes to use a specific existing content type. But for a user who plans to regularly use lots of title-less nodes, it may be a great solution.

Van's picture

Provided you working with a solid bg colour, set the H1 font to have the same colour in the CSS and make it tiny, this way SEO stil works, but its invisible... easy. 

Drupalace's picture

One of the easiest ways yet; thanks! This is good not only to keep the title visible to web crawlers, but is also good for anyone who, for whatever reason, wants the title area itself to appear as is, just without words visible. (Note too that readers will be able to see the title by selecting text in the area, should that matter at all to the site builder.)

Unknown Drupaloid's picture

Under no circumstances should you make invisible text (blk text on blk bkgd). Crawlers hate it and see it as a black hat technique.

Drupalace's picture

Actually, that's a really good point re SEO. It recalls those early days when shady site designers would stuff oceans of keyword text into the bottom of pages, "hidden" only by text color matching the background color. I can fully understand Google dinging a site's ranking for that.

So, let's clarify the recommendation: Making a page title "invisible" through coloring is fine as a quick and simple technique for a site – if it isn't concerned with SEO, or if the affected pages won't be crawled anyway.

Thanks for the reality check!

samual's picture

I'm still trying to figure the best way to display custom content using views, cck, the nodeteaser on your recommendation, and my custom regions that I have in my homepage layout.

Reseller Hosting Review

güvenlik's picture

Thank you for articles

fleshberkowski's picture

thanks for the thorough explanation!

hussain's picture

i want to right banner is show only main page and hide right bannre another page

 throw jumla

plz tell me this problem

thanku

Drupalace's picture

Not certain what you mean, but are you referring to displaying a sidebar on some pages, and not on other pages? If so:

There may be a variety of ways to do so, including using separate page.tpl.php for different pages, but I think the standard, normal Drupal way to do this would be to enable the sidebar's blocks only for the desired pages. That is, from admin/build/block, select "Configure" for each targeted block, and choose where the block will and won't appear under "Show block on specific pages:"

Is that the goal?

Unknown Drupaloid's picture

i really like your website

Deric Braito's picture

Quick and easy... I love it.

Coffee Bean's picture

Excellent blog! I actually love how it is easy on my eyes and also the facts are well written. I am wondering how I may be notified whenever a new post has been made. I have subscribed to your rss feed which must do the trick! Have a nice day!

golzbein's picture

Some of these comments people are saying here on this issue really seem to make me look at this in a whole new way. I do enjoy the responses though.
 

thecoolbeane's picture

I wanted a way to hide the title, selectively, on any page.

Renaming every page to 'Welcome' would work with the above code, but it doesn't help you when you are trying to manage your content - every node that is hidden has the same name. So I modified the code like this:

replace this line in page.tpl.php:

<h1 class="title"><?php print $title; ?></h1>

with this:

<?php if (substr(rtrim($title),-4,4) !="hide"): ?>

<h1 class="title"><?php print $title ?></h1>

<?php endif; ?>

PRESTO!! Now any page name that ends with 'hide' does not display the title. I can name pages, and hide/unhide the title at will, and still navigate through my content with meaningful names.

I hope this helps.

-beane

Drupalace's picture

I like! As you say, this should do the trick for any given title just by adding a certain word on the end, with no need to change the entire title to a certain word. Thanks!

Unknown Drupaloid's picture

Hey, this code made my day ;)

Thank you!

pimhof's picture

cellulite exercises's picture

Why have you got an exclamation mark at the beginning of "!How to hide a node title?"?

I was just wondering :)

Drupalace's picture

A fine question! The "!" was a test of a suggested method for hiding the title: see this comment

Now that I'm no longer experimenting with that method, I should remove the "!" from the title. And I should refresh the article with a summary of the many suggestions made by commenters! Will do. Thanks for the reminder!

Alex's picture

Keep it simple rather then mucking around with code, install this module then configure it.......... Job done in 2 secs :)

http://drupal.org/project/exclude_node_title

Cool

 

Send the chocolates in the post thanks ..

 

 

Drupalace's picture

Chocolate ThanksI suspect this module didn't yet exist when my post appeared; I'm glad to see it now. While lots of Drupalers prefer solutions all-around that don't involve the potential memory and speed hits of a module, I and many others are at least willing to try a module that offers to do the job easily, with no further mucking about. This one sounds good!

Thanks for the tip. I toss this milk chocolate keyboard in your direction!

Unknown Drupaloid's picture

thanks a lot

PJDNYC's picture

excellent tip - can highly recommend the module - installed and working perfectly in drupal 7

Add new comment