php, Drupal developer, CodeIgniter - freelance web developer

Drupal 7: Theming the taxonomy term page

By default the taxonomy term page in Drupal is accessible on site.com/taxonomy/term/TID and every node is closed in div tags. How to change that so it become a list (ul)? The lazy way is to install Views and use it's way to alter taxonomy term page. But there is easest way - using new fauture in Drupal 7 to change generated output before it be shown (renderable arrays). In template.php we have to add (if not) function THEME_preprocess_page(&$vars) like this:

function THEME_preprocess_page(&$vars) {
  // declarate list items variable
  $items = array();

  // check if current page is taxonomy term page 
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && !arg(3)) {

    // term load 
    $term = taxonomy_term_load(arg(2));

    // copy content so for later use
    $nodes = $vars['page']['content']['system_main']['nodes']; //

    // generate list items
    foreach ($nodes as $key => $node) {
      if (is_numeric($key)) $items[]['data'] = render($node);
    }

  // we declare that this content should be themed like list items
    $vars['page']['content']['system_main']['nodes'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
      '#attributes' => array('id' => 'THE_UL_ID', 'class' => array('THE_UL_FIRST_CLASS', 'THE_UL_SECOND_CLASS')),
    );
  }
}

And thats it. More info about list you can find here theme_item_list

P.S. You may have to clear the Drupal's cache.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.

www.stefanpetrov.net © 2007 - 2017All rights reserved

powered by Drupal European hosting Port Monitor