How to determine the current node type?

sisyphus - April 13, 2008 - 02:42

suppose i create a block and want to print out something when the content is showing a certain type of node, say, blog. how to code it in the block? i wrote this:

<?php
print $node->type;
?>
and show nothing. how to get the current node type?

thanks a lot.

looks like the right code.

leadstudios - April 13, 2008 - 03:17

looks like the right code. where did you put the print statement?

Blocks do not have a $node

nevets - April 13, 2008 - 03:33

Blocks do not have a $node variable, if you want the block to do something based on the current node you need something like

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
 
$node = node_load(arg(1));
  if (
$node->type == 'xyz' ) {
   
// Do something for nodes of type 'xyz'
 
}
}
?>

Thanks for the tip.

Rob T - June 4, 2008 - 06:29

Thanks for the tip, nevets.

 
 

Drupal is a registered trademark of Dries Buytaert.