Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughDisplaying 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
Edit node.tpl.php to add the content type name within a span with a class name (e.g. 'content-type-name') and...
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.
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; ?> — <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') ? ' — <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'))) ? ' — <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.
Last edited by Caroline Schnapp about 5 years ago.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web