0
votes

Quick version: How do I output a taxonomy term ID in a views template? I just want the numeric ID value. This will be used as a link anchor.

Long version:

Scenario, I have a view which displays a list of taxonomy terms. That view has a Page and a Block. The Page view is set to display the Block view as a header. That Block view simply contains the taxonomy names. The Page view displays all of the taxonomy content.

I want the Block view list to anchor to the items in the Page view:

Category page screenshot

This view is already built, the missing part of the equation is getting the anchor links in place.

The view currently comprises 3 custom template files:

views-view-fields--categories--page.tpl.php

<article id="NEED THE TERM ID HERE">
  <header>
    <h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
  </header>
  <div class="table">
    <div class="table-cell">
      <?php print $fields['field_category_image']->content; ?>
    </div>
    <div class="table-cell">
      <?php print $fields['description']->content; ?>
    </div>
  </div>
</article>

views-view-fields--categories--block.tpl.php

<li><a href="NEED THE TERM ID HERE"><?php print $fields['name']->content; ?></a></li>

views-view--categories--block.tpl.php

<ul>
    <?php print $rows; ?>
</ul>

I've tried using a views contextual filter rewrite on the top block view links, with no luck.

All I need is the variable for the TERM ID - I've done a var dump of the available variables, I can see the TID in that list, but have no idea how to reference it in a views-view-fields template file and can find nothing online that answers this most simple of concepts.

Screenshots of the Page and Block view setup:

Block viewPage view

2

2 Answers

2
votes

Finally won my argument that this jump list is completely redundant and stupid, so it'll be removed, however, I did manage to output the TID, which was fairly obvious, as these things often are...

views-view-fields--categories--block.tpl.php

<li>
    <a href="#cat<?php print($view->result[$view->row_index]->tid); ?>"><?php print $fields['name']->content; ?></a>
</li>

views-view-fields--categories--page.tpl.php

<article id="cat<?php print($view->result[$view->row_index]->tid); ?>">
  <header>
    <h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
  </header>
  <div class="table">
    <div class="table-cell">
      <?php print $fields['field_category_image']->content; ?>
    </div>
    <div class="table-cell">
      <?php print $fields['description']->content; ?>
    </div>
  </div>
</article>

The variable is obviously in the view array, it was just a case of getting the index of the current view item.

0
votes

Adding

<?php print $fields['tid']->content; ?>

should give you the TID in views-view-fields--xxx--page.tpl.php and --block.tpl.php

Make sure the fields are set to remove any default wrappers and you should be good to go.