3

How can I hide the title on a specific node?

I have tried the following three modules, but none of them do what I'd like.

  1. Page Title

  2. Exclude node title

  3. Automatic Node titles

I added field_hide_title to the page content type, the code in hook_preprocess_node() function. It can't work fine.

screenshot

The value shown from print_r($vars['node']) is the following:

stdClass Object(
  [vid] => 4
  [uid] => 1
  [title] => search_material
  [log] => 
  [status] => 1
  [comment] => 1
  [promote] => 0
  [sticky] => 0
  [nid] => 4
  [type] => page
  [language] => zh-hant
  [created] => 1339055801
  [changed] => 1339727555
  [tnid] => 0
  [translate] => 0
  [revision_timestamp] => 1339727555
  [revision_uid] => 1
  [body] => Array(
    [und] => Array(
      [0] => Array(
        [value] =>
        [summary] =>
        [format] => php_code
      )
    )
  )
  [field_hide_title] => Array(
    [und] => Array(
      [0] => Array([value] => 1)
    )
  )
  [rdf_mapping] => Array(
    [rdftype] => Array([0] => foaf:Document)
    [title] => Array(
      [predicates] => Array([0] => dc:title)
    )
    [created] => Array(
      [predicates] => Array(
        [0] => dc:date
        [1] => dc:created
      )
      [datatype] => xsd:dateTime
      [callback] => date_iso8601
    )
    [changed] => Array(
      [predicates] => Array([0] => dc:modified)
      [datatype] => xsd:dateTime
      [callback] => date_iso8601
    )
    [body] => Array(
      [predicates] => Array([0] => content:encoded)
    )
    [uid] => Array(
      [predicates] => Array([0] => sioc:has_creator)
      [type] => rel
    )
    [name] => Array(
      [predicates] => Array([0] => foaf:name)
    )
    [comment_count] => Array(
      [predicates] => Array([0] => sioc:num_replies)
      [datatype] => xsd:integer
    )
    [last_activity] => Array(
      [predicates] => Array ([0] => sioc:last_activity_date)
      [datatype] => xsd:dateTime
      [callback] => date_iso8601
    )
  )
  [cid] => 0
  [last_comment_timestamp] => 1339055801
  [last_comment_name] =>
  [last_comment_uid] => 1
  [comment_count] => 0
  [name] => admin
  [picture] => 0
  [data] => b:0;
  [entity_view_prepared] => 1
)

============================================================ UPDATE:

but I found one thing. before I change the title to NULL, the title has been displayed out...

print_r($vars) in mytheme_preprocess_page function:

[summary] => [format] => php_code ) ) ) [field_hide_title] => Array ( [und] => Array ( [0] => Array ( [value] => 1 ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date 1 => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1339039221 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 ) )

  • Do you wan't to hide the title for all nodes or just one specific? – Cyclonecode Jun 14 '12 at 7:39
  • I don't want to modify template because it will cause effect all node. – user6919 Jun 14 '12 at 7:43
  • You can change the template per node TYPE. Why not just use CSS? .page-node-NID .page-title { display: none;} – Chapabu Jun 14 '12 at 8:17
  • 1
    IMHO..doing it via template.php and css for every node is a good idea if the node you want to hide the title is specific. But if you are creating a new node constantly and some of it should not display node title then you need to update your code regularly which will point to maintenance issue. You will ended up having long code. This question is related to my task before which I ended up creating a cck field so the editors of the site can tick the checkbox if he dont want to appear the Title or Headline for some reason. – ninjascorner Jun 14 '12 at 8:35
  • Doing it via CSS is especially recommendable if you care for site ranking. To my knowledge, titles in header tags are one of the strongest ranking boosters. – Artur Jun 14 '12 at 11:21
16

You can create a cck field(checkbox/boolean) for that will act as a flag. So you can have a dynamic option if you want to hide the node or not.

Pseudo code:

In template.php

function yourtTheme_process_page(&$variables) {
  if($variables['node']->field_hide_title['und'][0]['value']) {
    $variables['title'] = NULL;
  }
}

Haven't try this code but I think this will work.

EDIT: Added screenshot of a new field enter image description here

This should be the value of your cck enter image description here

3

Use CSS.

Each individual node will have a specific class on the <body> tag of the page, which will allow you to target your specific node.

For example, say you're trying to hide the title on node 69, it would look like the following:

.page-node-69 .page-title { 
  display: none;
}

for node 150 it would look like this:

.page-node-150 .page-title { 
  display: none;
}

You might have to tweak it depending on your theme, and you might have to more specifically target the page title (i.e. h1.page-title) just in case there is more specificity further up the cascade.

2

I guess you could add something like this into your template.php:

function YOURTHEME_preprocess_node(&$node) {
    // check if it's the node of interest
    if($node['title'] == 'your-specific-node-title') {
       // set the title to NULL so nothing is rendered
       $node['title'] = NULL; 
    }
}

As chapabu suggested it might be a better idea to check the node nid instead of the title:

if($node['nid'] == 'your-node-nid') {
   ...
}
  • Would be nice if you could specify why downvoting? – Cyclonecode Jun 14 '12 at 8:03
  • I didn't downvote, but processing the node based on NID is probably more reliable than title. I shall give this an upvote to counteract the downvote :-) – Chapabu Jun 14 '12 at 8:14
  • @Chapabu - Yes of course the node id makes more sense than the title. – Cyclonecode Jun 14 '12 at 8:21
  • It seems not convenient. :( – user6919 Jun 14 '12 at 8:21
  • So it works...but it's not convenient for you to implement? – Chapabu Jun 14 '12 at 8:23
0

The CSS on the title is a class of title and ID of page-title, so my CSS needs to be

.page-node-69 #page.title {
   display: none; 
}

for this to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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