Syndicate

Feed

Displaying the content type name

You want to print the content type name along with the 'submitted' info. This will be particularly useful for those who have created custom content types, with or without CCK.

Solution

  1. Edit node.tpl.php to add the content type name within a span with a class name (e.g. 'content-type-name') and...

  2. style that span element in your theme's style.css file.

In the following example, the human-readable name for the content type is 'News', and the content type is displayed to the right of the 'submitted' info. Let's try and achieve that.

Node with displayed content type name next to the submitted info.

Your file node.tpl.php may look like this before the edit (I am only providing a snippet of the template here):

<?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted; ?></span>
<?php endif; ?>

If it does, then you will change the above snippet to that:

<?php if ($submitted): ?>
  <span class="submitted"><?php print $submitted; ?> &mdash; <span class="content-type-name"><?php print node_get_types('name', $node); ?></span></span>
<?php endif; ?>

You may want to exclude a certain content type. For example, you may not want 'page' to appear in page nodes. Whenever you want to exclude one content type, you will do like so:

<?php if ($submitted): ?>
  <span class="submitted"><?php print $submitted; ?><?php print ($node->type != 'page') ? ' &mdash; <span class="content-type-name">' . node_get_types('name', $node) . '</span>' : ''; ?></span>
<?php endif; ?>

When you want to exclude more than one content type, say 'story' and 'page', you will do like so:

<?php if ($submitted): ?>
  <span class="submitted"><?php print $submitted; ?><?php print (!in_array($node->type, array('story', 'page'))) ? ' &mdash; <span class="content-type-name">' . node_get_types('name', $node) . '</span>' : ''; ?></span>
<?php endif; ?>

This will work in Drupal 5 as well. Again, this solution works only for PHPTemplate-powered themes.

You need to provide the machine-readable names for content types you wish to exclude, because $node->type is used in the condition, and it is the machine-readable name for the content type. Why are we comparing machine-readable names? Simply to avoid database queries. There is one database query we cannot avoid in our solution, it is the one query we execute to print the human-readable name for the content type.

Possible variations: place the content type information somewhere else in the node; display the content type information only in the teaser; etc.

The machine-readable name of a content type is usually not the same as its human-readable name. Sometimes the difference is only in capitalization:

The machine-readable name here is news, while the human-readable name is News.

We used the Drupal function node_get_types() in our solution.

The function can be used in so many ways that it gets rather confusing, as webchick points out here. The function can be used in these seven different ways:

node_get_types('type', $node);
node_get_types('type', $node->type);
node_get_types('names');
node_get_types('name', $node);
node_get_types();
node_get_types('types', NULL, TRUE);
node_get_types('module', $node);

When we use the function like so:

node_get_types('name', $node);

... the returned value is a string, and it is the human-readable name for the content type of the $node object.

By the way, the $node object is available in all these templates:

  • comment.tpl.php
  • node.tpl.php
  • page.tpl.php when a node is displayed on its dedicated page, ie: at node/x

In Drupal 7, there will be a new function to get the human-readable name for the content type of a node:

$name = node_get_name($node);

Until then, we'll make-do-and-mend with node_get_types().

Last edited by Caroline Schnapp about 13 years ago.

Comments

Give a big hand!

The primordial purpose of introduction of commodities markets is to provide liquidity and hedging opportunities to farmers and traders and effective enforcement to manage sound risk. Commodity market is gaining prominence similar to the equity market. More Information about Best Tips on Gold Mini MCX visit my site 100mcxtips.com.

Ang Mo Kio Condo Launch

Ang Mo Kio Condo Launch comprises of TBA units in total with TBA Blocks and TBA storeys. Future residents are within a short driving distance to Ang Ko Kio Hub and Compass Point in Ang Mo Kio. Ang Mo Kio Condo panaroma

Several buses are available

Several buses are available near The Inflora Condo along with shopping centers and restaurants. The Inflora Condo is also near to Tanah Merah Golf & Country Club and Safra Golf & Country Club. The Inflora

naturalbeautyguides.blogspot.com

Expert advice on beauty tips, perfumes, prestige cosmetics, fragrances, makeup, skin care, hair care and gifts. naturalbeautyguides.blogspot.com

Rivertrees Residences is a

Rivertrees Residences is a 99-years leasehold private condominium development located at Sengkang West Way / Fernvale Link in District 19. With expected completion in mid 2017, it comprises of TBA towers with estimated 590 units and stands TBA storeys tall. Rivertrees Condo

Pet Grooming Centres in Singapore

There are many different instruments in dog grooming singapore to groom dogs. Among them is the Curry Brush, Shedding Blade, Stripping Knives, Slicker Brushes and Rakes. These tools are very common among dog groomers and every pet shop should be able to supply. Pet Grooming Singapore