node.tpl.php - node_invoke($node->type, 'node_name')

JohnG - March 14, 2006 - 23:54

(copied from handbook http://drupal.org/node/11816 - I thought it might be better to discuss this in the forums rather than risk a lengthy thread on a handbook page which currently benefits from clarity!)

content type name
JohnG - March 6, 2006 - 04:25

Despite this question going unanswered many times in the forums, I found the answer (I think) in Moshe's organic groups module.

Place this at the top of your node.tpl.php:

<?php
  $type
= (node_invoke($node->type, 'node_name'));
?>

and then call

<?php print $type ?>

where you want it inline.

this gives you the content type label rather than the module name, so it's much better than the traditional hack: <?php print $node->type ?>

It works for me!

=======================

Update For Drupal 4.7
alexis - March 13, 2006 - 20:23

Just wanted to add that the proper code for 4.7 would be:

<?php
  $type
= (node_invoke($node->type, 'node_info'));
?>

and then

<?php print $type[$node->type]['name']; ?>

Alexis Bellido - Ventanazul web solutions

==========================

Works for me, too. Don't
zwhalen - March 13, 2006 - 20:11

Works for me, too. Don't know what it means in terms of processing time or db usage or whatever, but it seems to work well. Thanks!

overheads

JohnG - March 14, 2006 - 23:59

I had wondered about Zwalen's question about the 'overheads' of this method too. I guess a function in the node.tpl.php is going to get called when almost every page loads, so the function does need to be pretty slick ...

From what I can see from the API references http://drupaldocs.org/api/4.6/function/node_invoke seems to find the data in some part of the $node array (?) ... at least (AFAIK) it doesn't do any $sql database queries.

I would any appreciate advice on this one too.

thank you

Chill35 - March 1, 2007 - 16:54

Very sleek.

Caroline
Who am I | Where are we
11 heavens

OR...

Chill35 - March 1, 2007 - 17:12

<?php
print node_get_name($node->type);
?>

Definition of node_get_name() : Determine the human readable name for a given type.
Parameters : Either a node object, a node array, or a string containing the node type.
Return value : The human readable name of the node type.
Source : http://api.drupal.org/api/4.7/function/node_get_name

Code :

<?php
function node_get_name($node) {
  return
_node_names('name', $node);
}
?>

Does that function make a db_query ? From the look it, I don't think so :
http://api.drupal.org/api/4.7/function/_node_names

More stuff in our toolkit!

Thanks John, I just saw this and I though it may interest you. ONE year later! LOL...

That's for 4.7.x.

Caroline
Who am I | Where are we
11 heavens

ah - the fruits of progress

JohnG - March 1, 2007 - 23:48

ah - the fruits of progress

:)

For Drupal 5

tic2000 - March 2, 2007 - 23:56

To get the human readable name of a content type in Drupal 5 just add in your node.tpl.php

<?php
print node_get_types('name', $node)
?>

Even better

Chill35 - March 3, 2007 - 02:47

I find. Thanks.

Caroline
Who am I | Where are we
11 heavens

Wow! That easy hey!

LeDucDuBleuet - June 18, 2007 - 04:18

Thanks! This is a must when you have many content types showing on the front page as it becomes confusing to guess what is what then...
LDdB

Easy

rubyji - January 6, 2008 - 20:32

Thanks!

In Drupal 5 and 6

Chill35 - June 16, 2008 - 23:21

Relying on this function instead: http://api.drupal.org/api/function/node_get_types ...

<?php
print node_get_types('name', $node);
?>

If you don't want 'page' to appear for pages, use this:

<?php
print $node->type != 'page' ? node_get_types('name', $node) : '';
?>

If you wish to exclude more than 1 content types from being printed (their name, that is!), use something like this:

<?php
if (!in_array($node->type, array('story', 'page'))) { print node_get_types('name', $node); }
?>

Caroline

11 heavens.com

How to work with i18n "Content Types" module?

isis - August 24, 2008 - 12:24

node_get_types( ) is nice, but is there another way to display node-types in different languages works with the i18n "Content Types" module?

Much thanks. :)

 
 

Drupal is a registered trademark of Dries Buytaert.