Hi,

I'm trying to display breadcrumbs only on pages with a certain content-type.
This is the code in question:

<?php if($node->type == 'type1' || $node->type == 'type2'): ?>
                            <div>
                                <?php print $breadcrumb; ?>
                            </div>
<?php endif; ?>

which for type2 outputs the breadcrumbs as intended, but for type1 results in a notice stating 'node' is undefined and the output of just the 'Home' - link.

I've read this:

$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).

(http://api.drupal.org/api/drupal/modules%21system%21page.tpl.php/7)

but I don't see in what way the two content types differ...

Thanks for any suggestions,
eR

Comments

tce’s picture

Do you know how to create a module? If so, I personally would do something like:

<?php
/**
 * Implements hook_node_view().
 */
function MODULENAME_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'type1' || $node->type == 'type2') {
    drupal_set_breadcrumb();
  }
}
?>

if not, you can do it in your themes template.php file via hook_preprocess_page():

<?php
/**
 * Override or insert variables into the page template.
 */
function THEMENAME_process_page(&$variables) {
  if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {
    $node = node_load(arg(1));
    if ($node->type == 'type1' || $node->type == 'type2') {
      $variables['breadcrumb'] = NULL;
    }
  }
}
?>
enochRoot’s picture

thank you for the suggestions, I will probably try both in different contexts...
right now I'm still hoping for a social hacking solution (i.e. convince someone that it's a stupid idea to have breadcrumbs only on specific pages)

;)

eR

tce’s picture

I agree, consistency will always make it easier for any site user, so if they are expecting a breadcrumb but not seeing it, their heads might explode.