10

In Drupal 7, taxonomy.pages.inc contains the taxonomy_term_page(), which places a <div class="term-listing-heading"> around the taxonomy heading output.

How can I re-write the output of taxonomy_term_page() in my theme, so I can remove the DIV without hacking core?

I'm quite surprised that there isn't a tpl.php file available for taxonomy_term_page() as this would making theming much easier.

11

You can do it with preprocess page something like this:

function themename_preprocess_page(&$vars) {
  if  (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    unset($vars['page']['content']['system_main']['term_heading']['#prefix']);
    unset($vars['page']['content']['system_main']['term_heading']['#suffix']);
  }
}

in your theme's template.php

I believe system_main could be called something else, depending on your site setup.

6

As it is a menu callback, you can implement hook_menu_alter() in a module to alter the menu callback invoked for that page.

function mymodule_menu_alter(&$items) {
  if (!empty($items['taxonomy/term/%taxonomy_term'])) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = 'mymodule_term_page';
  }
}

function mymodule_term_page($term) {
  // Build breadcrumb based on the hierarchy of the term.
  $current = (object) array(
    'tid' => $term->tid,
  );
  $breadcrumb = array();

  while ($parents = taxonomy_get_parents($current->tid)) {
    $current = array_shift($parents);
    $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
  }
  $breadcrumb[] = l(t('Home'), NULL);
  $breadcrumb = array_reverse($breadcrumb);
  drupal_set_breadcrumb($breadcrumb);
  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  $build = array();

  $build['term_heading'] = array(
    'term' => taxonomy_term_view($term, 'full'),
  );

  if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) {
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes);
    $build['pager'] = array(
      '#theme' => 'pager', 
      '#weight' => 5,
    );
  }
  else {
    $build['no_content'] = array(
      '#prefix' => '<p>', 
      '#markup' => t('There is currently no content classified with this term.'), 
      '#suffix' => '</p>',
    );
  }
  return $build;
}
  • Thanks! I prefer googletorp's method as it doesn't need a separate module. But your suggestion is great as it gives more control. Thanks! – big_smile Jun 16 '11 at 13:19
  • As far as I recall, hook_menu_alter() can be implemented in a theme too; in Drupal 7, themes can implement alter hooks. – kiamlaluno May 30 '12 at 4:47
2

Like the previous example, except it may be cleaner and more future proof to modify the returns from taxonomy_term_page in a wrapper rather than copy the original function wholesale:

function mymodule_menu_alter(&$items) {
  if (!empty($items['taxonomy/term/%taxonomy_term'])) {
    $items['taxonomy/term/%taxonomy_term']['page callback'] = '_custom_taxonomy_term_page';
  }
}

function _custom_taxonomy_term_page ( $term ) {

   $build = taxonomy_term_page( $term );

   // Make customizations then return
   unset( $build['term_heading']['#prefix'] ); 
   unset( $build['term_heading']['#suffix'] );

   return $build;
}
  • This is probably the best way to do it. – Horatio Alderaan Aug 8 '12 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.