0

I have taxonomy-term.tpl.php page in my theme folder which shows the standard layout of a taxonomy term page like : Title, Description and content listing corresponding to the taxonomy term. Now I wish override this standard layout for a particular term and I create another term file namely : taxonomy-term--32.tpl.php. For this I haven't added any function in the template.php file in my theme.

My problem is that for the term page with tid / term id = 32, does not entirely override the content block. Precisely, the listing of the contents related to that taxonomy term remains in the overridden web page as well, which is not what I want.

N.B : I have disabled the Taxonomy Term View from the admin end of the site as I am not absolutely sure about its functionality.

  • there is a possibility that Drupal use its cache rather than the new templates as suggested, clear your caches – Petro Popelyshko Apr 24 '13 at 8:20
  • What you want to do actually ? – niksmac Apr 24 '13 at 8:26
  • That template only governs the display of the taxonomy term itself, as you seem to have realised. The $content array is simply a list of rendered nodes. If you want to override it, Views would be the simplest way to proceed. Otherwise you are likely to require some fairly messy and hard-to-maintain code. – Alfred Armstrong Apr 24 '13 at 8:53
0

I don't want to simply edit my first answer because I think that's just as feasible an option as this one is (and less complicated). Regardless, this should do the trick. Basically all we're doing is modifying the page callback for the taxonomy/term/%taxonomy_term menu item.

1) Inside the sites/all/modules directory, create a folder (directory) called newtaxonomypages and inside that folder, create two files: newtaxonomypages.info and newtaxonomypages.module.

2) Paste the following code inside newtaxonomypages.info:

name = New Taxonomy Pages
description = Changes the display of taxonomy/term pages.
core = 7.x
dependencies[] = taxonomy

3) Paste the following code inside newtaxonomypages.module:

<?php

/**
 * @file
 * Alters the page callback for the menu item taxonomy/term/%taxonomy_term.
 */

/**
 * Implements hook_menu_alter().
 */
function newtaxonomypages_menu_alter(&$items) {
  // Change page callback to 'newtaxonomypages_page' from 'taxonomy_term_page'...
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'newtaxonomypages_page';
  // ... which was originally found in a separate file, but now it's defined below. So:
  unset($items['taxonomy/term/%taxonomy_term']['file']);
}

/**
 * The code inside this next function is the exact same code that you'd find on line 17 of
 * modules/taxonomy/taxonomy.pages.inc, except the nodes have been commented out and removed
 * from the view.
 */
function newtaxonomypages_page($term) {
  // If there is a menu link to this term, the link becomes the last part of
  // the active trail, and the link name becomes the page title. Thus, we must
  // explicitly set the page title to be the term title.
  drupal_set_title($term->name);

  // Build breadcrumb based on the hierarchy of the term.
  $current = (object) array(
    'tid' => $term->tid,
  );
  // @todo This overrides any other possible breadcrumb and is a pure hard-coded
  //   presumption. Make this behavior configurable per vocabulary or term.
  $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);

  // Set the term path as the canonical URL to prevent duplicate content.
  $uri = entity_uri('taxonomy_term', $term);
  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  // Set the non-aliased path as a default shortlink.
  drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);

  // Normally we would call taxonomy_term_show() here, but for backwards
  // compatibility in Drupal 7 we do not want to do that (it produces different
  // data structures and HTML markup than what Drupal 7 released with). Calling
  // taxonomy_term_view() directly provides essentially the same thing, but
  // allows us to wrap the rendered term in our desired array structure.

  $build['term_heading'] = array(
    '#prefix' => '<div class="term-listing-heading">',
    '#suffix' => '</div>',
    'term' => taxonomy_term_view($term, 'full'),
  );

  // *****PART THAT HAS BEEN REMOVED: START***** //
  /*
  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>',
    );
  }
  */
  // *****PART THAT HAS BEEN REMOVED: END***** //

  return $build;
}

4) Save both files, go to admin/modules, find the New Taxonomy Pages module (under Other), enable it, then Save configuration.

  • Thanks a ton yet again @jerdiggty. I can't understand why changes in the node--page.tpl.php doesn't reflect in my site. Nevertheless, heartiest appreciation for the help that you did provide me twice over. I am working with Drupal over the past three months, and I have a lot to catch up. Hope you will be there in days to come. Cheers mate :) – Rajat Bandyopadhyay Apr 25 '13 at 14:30
0

Change your node.tpl.php file so that it only returns the parts of the node you want, if the current path of the page being viewed is taxonomy/term. Using the Bartik node.tpl.php file as an example, and assuming you only want the title (linked to the node) to be displayed, you could do something like this:

Original ("before"):

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?></a>
    </h2>
  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>
    <div class="meta submitted">
      <?php print $user_picture; ?>
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

  <div class="content clearfix"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>
  </div>

  <?php
    // Remove the "Add new comment" link on the teaser page or if the comment
    // form is being displayed on the same page.
    if ($teaser || !empty($content['comments']['comment_form'])) {
      unset($content['links']['comment']['#links']['comment-add']);
    }
    // Only display the wrapper div if there are links.
    $links = render($content['links']);
    if ($links):
  ?>
    <div class="link-wrapper">
      <?php print $links; ?>
    </div>
  <?php endif; ?>

  <?php print render($content['comments']); ?>

</div>

Altered ("after"):

<?php if (!$page && arg(0) == 'taxonomy' && arg(1) == 'term'): ?>
<?php
 // Note:
 // If you want to see all the values available to you at this point, uncomment
 // the next line (not safe for production environments):
 // print '<pre>' . check_plain(print_r($node, 1)) . '</pre>';
?>
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?></a>
    </h2>
  <?php print render($title_suffix); ?>
</div>

<?php else: ?>

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?></a>
    </h2>
  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>
    <div class="meta submitted">
      <?php print $user_picture; ?>
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

  <div class="content clearfix"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>
  </div>

  <?php
    // Remove the "Add new comment" link on the teaser page or if the comment
    // form is being displayed on the same page.
    if ($teaser || !empty($content['comments']['comment_form'])) {
      unset($content['links']['comment']['#links']['comment-add']);
    }
    // Only display the wrapper div if there are links.
    $links = render($content['links']);
    if ($links):
  ?>
    <div class="link-wrapper">
      <?php print $links; ?>
    </div>
  <?php endif; ?>

  <?php print render($content['comments']); ?>

</div>
<?php endif; ?>

Hope that helps...

:)

  • Im using a path auto module in my site for which, the immediate url that we see does not contain taxonomy/term. And as for the theme, a customized sub-theme of Omega is being used. We have a node--page.tpl.php page apart from the normal node.tpl.php in the node folder within the theme folder also and overriding both in accordance to your post did not make any difference. All I want is to hide the listing in the taxonomy-term pages that I override for particular catregories by creating new taxonomy-term--[tid | machine_name_of_terms].tpl.php. Is that possible anyhow? – Rajat Bandyopadhyay Apr 24 '13 at 10:12
  • @RajatBandyopadhyay Don't worry... One of the (many) awesome things about Drupal is that using aliases (with or without pathauto) should not change the value of arg(0) or any arg() for that matter. i.e. if you create a page at node/1 and then give it an alias of home, arg(0) is still going to be node and arg(1) is still going to be 1. As far as using custom file templates, you could certainly apply the same structure to your node--page.tpl.php file, since what you see on the taxonomy/term pages is a list of node teasers. (Continued in next comment...) – jerdiggity Apr 25 '13 at 3:02
  • You could also do what you suggested, using taxonomy-term--[tid | machine_name_of_terms].tpl.php files, although I believe the node teasers will still appear. For more control, consider using files that start with "page--". More possible files: page--taxonomy--term--[term_id].tpl.php, page--taxonomy--term--%.tpl.php, page--taxonomy--term.tpl.php, page--taxonomy.tpl.php, taxonomy-term--[term_id].tpl.php, taxonomy-term--[machine_name].tpl.php, etc. I'd also highly recommend playing around with the devel_themer module. – jerdiggity Apr 25 '13 at 3:06
  • Thanks for all your support, @jerdiggity. I have tried also tried the page--term--tid.tpl.php way, but in that path I'll have to rebuild the entire page every time I override a particular taxonomy term. I want to override the main content block differently for different taxonomy pages, not the entire page (header, one-sidebar & the footer). (continued in next comment ...) – Rajat Bandyopadhyay Apr 25 '13 at 5:46
  • While trying to find out ways to hack the taxonomy pages, I found out a module which might be resposnsible for the standard content listings even on the overridden taxonomy pages - the Entity View Mode module (Administrator > COnfigurations > System > Entity View Modes in Drupal #7). I have a option there as Taxonomy Term Page, but the View Mode is locked for any operations. Can you throw some light upon the same, if you have encountered such a circumstance anytime. Cheers ! – Rajat Bandyopadhyay Apr 25 '13 at 5:47

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.