Hi there,

I'm having a "scratch my head" day with this one. I'm trying to hide the node's title for certain pages.
In Drupal 6 I was using the Barlow theme and I copied the node.tpl.php file to node-notitle.tpl.php.
"notitle" is the machine readable name for content type nodes without titles. In this file I simply commented out the "
<?php print $title; ?>
" and saved and it worked great.

Now I installed Drupal 7, using the Bartik theme. When I comment out " <?php print $title; ?>
" in node.tlp.php, it removes the title from the front page, however it still shows in the actual node. - so that didn't work.

When I comment out " <?php print $title; ?>" in page.tpl.php it removes the comment for all the nodes, which means this must be the right place to comment out that line. So my content type machine readable name is "notitle", and I thought by making a copy page.tpl.php to page-notitle.tpl.php, and commenting out " <?php print $title; ?>" that it would work, but it simply just doesn't work.

Is there anyone that could please give me advise on this? I've spent many hours just trying to find a solution to this for Drupal 7 and came up with nothing. It might be that I'm misreading something, or not understaning it - I don't know, but could anyone please help me????

Thanks in advance,

mseraphim

Comments

Kirk’s picture

node--notitle.tpl.php

Is the template suggestion for Drupal 7.

More info on changes here http://pingv.com/blog/a-peek-at-drupal-7-theme-system-changes or on the googles.

mseraphim’s picture

Hi Kirk,

Thanks a million, however it is not the node.tpl.php that needs to change, it's the page.tpl.php.
I have tried following your suggestion with copying page.tpl.php to page--notitle.tpl.php and commenting out <?php print $title; ?> , however this didn't seem to work at all.

I'm sure the machine code for the content type it "notitle". It is as if it's not recognizing the page--notitle.tpl.php file.

Any other suggestions? Have you tried this on your side?

Thanks a again for your reply...

Regards,

mseraphim

Kirk’s picture

As far as I know, page--[contenttype].tpl.php is not a default template suggestion. In order to override the page template for a content type, you have to do something like this http://drupal.org/node/1035656#comment-3996684 I haven't personally tested that, but based on the comments it seems to work.

Alternately, if the only difference between the 2 content types is the title display, you can do that with a simple IF statement.

<?php if ($title && $node->type != 'notitle'): print $title; endif; ?>

That is the approach I would personally take so I don't have 2 templates to manage for a minor difference.

mseraphim’s picture

This worked perfectly! Thank you so much for this little php snippit. I'm don't know enough about the php language to do this myself, but you were a fantastic help. This is definitely something that should have been included in Drupal 7 Core.

I had to adapt the code a little bit to fit the standard Bartik theme. Here is the code:

<?php /*?>This below checks for 'notitle' content type and then it doesn't print the title, however it still prints the normal title for page content type<?php */?>

      <?php if ($title && $node->type != 'notitle'): ?>
        <h1 class="title" id="page-title">
          <?php print $title; ?>			  
        </h1>
      <?php endif; ?>

Once again, thanks and I hope that this could help many folks in the future!

Regards,

mseraphim

mseraphim’s picture

The above worked perfectly fine when I viewed the site, but when I logged in I got this message:

* Notice: Undefined variable: node in include() (line 191 of C:\wamp\www\themes\bartik\templates\page.tpl.php).
* Notice: Trying to get property of non-object in include() (line 191 of C:\wamp\www\themes\bartik\templates\page.tpl.php).

Now line 191 is:

      <?php if ($title && $node->type != 'notitle'): ?>

Any ideas?

mseraphim

Kirk’s picture

Works fine on Bartik for me, and I don't see any obvious issues with your PHP. Try clearing your caches perhaps?

oj235’s picture

I tried the above solution by posting mseraphim's code in page.tpl.php, but no joy for me. Not getting any errors either. I'm using Bartik as well and commented out the other instance of
print $title;

In fact, commenting that out does not appear to have any effect either. Any thoughts would be much appreciated. I just want to create a page with no title (like the front page and the login page). I'm new to PHP and drupal so bare with me if I am doing anything silly!

mseraphim’s picture

Hi oj235,

If you want to use the code that I posted you need to do the following - I hope this works for you.

You need to create a new content type. Call the content type 'notitle'. when you do this, make sure that the machine readable name for this content type is 'notitle'. Then this code should work. If it doesn't, please post your php file's code here. We could then have a look at it and let you know what you need to change.

Please let us know if it worked for you.

regards,

mseraphim

mseraphim’s picture

Simply put, I need certain pages on my website without node titles. There were a few suggestions that I tried to follow and it worked, except for one thing.

This is what I did:
1. I created a new content type and called it “notitle”. The machine readable name for it is also called “notitle”.
2. I then created a new node in this content type just to test it.
3. I opened the page.tpl.php file and did the following I changed the lines from:

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

To this:

<?php /*?>This below checks for 'notitle' content type and then it doesn't print the title,
          however it still prints the normal title for page content type<?php */?>
      <?php if ($title && $node->type != 'notitle'): ?>
        <h1 class="title" id="page-title">
          <?php print $title; ?>			  
        </h1>
      <?php endif; ?>

All of this worked great and it then didn’t display the title anymore for the “notitle” content type’s nodes, however…

4. When I log in as administrator I get the following error message:

* Notice: Undefined variable: node in include() (line 191 of C:\wamp\www\themes\bartik\templates\page.tpl.php).
* Notice: Trying to get property of non-object in include() (line 191 of C:\wamp\www\themes\bartik\templates\page.tpl.php).

The code for line 191 is the following:

      <?php if ($title && $node->type != 'notitle'): ?>

5. Do you have any suggestions please? I’m not very good with php, but certainly try my best. Any help would be great.

Thanks in advance.

Mseraphim

Pizzutz’s picture

You are using the node variable, but not all drupal pages display a node. Change 191 to

<?php if ($title && !($node && $node->type == 'notitle')): ?>

And it will check if there is a node before it goes and tries to access it's properties.

mseraphim’s picture

Hi Pizzutz,

I tried the coding you gave me, but there was still one error I received. On a different page Morbus Iff posted this information for me that worked perfectly.

Posted by Morbus Iff on March 9, 2011 at 6:41pm new

Not critical as it does not break all Drupal sites. The notices are ignorable, but if you'd like 'em gone, try replacing them
with the following (they're caused because page.tpl.php is used for *every Drupal page*, not just those that are nodes):

<?php if ($title && isset($node) && $node->type != 'notitle'): ?>

Regards,

Mseraphim

ataxia’s picture

This is working for me with the Fusion Starter theme.

I just added a list (text) field called "field_hide_title" to my "basic_page" content type, made it not required, the only option is "hide|Hide"

Now there is a "Hide Title" field on all pages with radio buttons for "N/A" and "Hide" that I can use to hide the titles on any page.

Then I changed this section in page.tpl.php:


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

to this:


<?php if ( isset ( $node->type ) && ($node->type == 'basic_page') &&  (isset ($node->field_hide_title[$node->language][0]['value'] )) )
   { $notitle = $node->field_hide_title[$node->language][0]['value']; }
else  $notitle = "" ;   ?>                             
                            
<?php if ($title): ?>
<h1 class="title<?php print $notitle;  ?> gutter"><?php print $title; ?></h1>
<?php endif; ?>

and added this to my local.css file:


H1.titlehide {
 display:none;
} 

Note: This is for a multilanguage site, so you might have to use 'und' instead of $node->language for the variable name


$notitle = $node->field_hide_title['und'][0]['value']; 

mseraphim’s picture

Hi ataxia

I am about to start a new site and will certainly try your code.

Will post back if I run into problems.

Regards,

mseraphim