Mmmmmmmm... cupcake« an older post
a newer one »Java on the Rocks

Adding Human Readable Content Type to Node display in Drupal 7


In the mismash of my site's content, I have a bunch of different content types on the front page, and it isn't clear what posts are what type. I've been reading a lot of books, so there are more book reviews than blog posts, and come on, who wants to read all of those reviews (hint: not me, I'm writing them so that I don't read the books again because I forgot that I read them the first time). So, on my vacation, I decided to get to the niggly little things that annoy me in various systems. First up: human readable content types in the posts.

I'm talking about these things:

Okay, so, to do this, in your theme's template.php file, add a node preprocess hook function. You may already have one. Look for THEMENAME_preprocess_node.

Add these two lines:

function THEMENAME_preprocess_node(&$vars) {
  $node_names = node_type_get_names();
  $vars['node_type_name'] = isset($node_names[$vars['node']->type]) ? $node_names[$vars['node']->type] : '';

This makes the variable $node_type_name available in the node template file. See node_types_get_names() for more details.

So, open the node.tpl.php file in your theme's template files. If you don't have one yet, copy it from modules/node/node.tpl.php into your theme directory and clear your theme registry cache.

In your node.tpl.php file, add these lines:

  <?php if ($node_type_name != '') { ?>
    <span class="title-type"><?php print $node_type_name; ?></span>
  <?php } ?>

I have it inside a if ($display_submitted): check, so it shows only when the submitted information displays. Your mileage may vary.

I styled the CSS using the .title-type class, cleared all caches, and done. Human readable content types displaying on nodes in Drupal 7

Add new comment