In furtherance of this thread: Display vocabulary description, which seeks a way to display a term's (category) description as the introduction text for each section, or category, within a site.

The goal:

  1. Site directory structure thus: www.example.com/category/index.htm and; www.example.com/category/sub-category/index.htm
  2. To display an introductory description for each category (term description) above all sub-page teasers
  3. Remove (HTML) code-bloat from the link (anchor) title attribute beneath each teaser (currently term description x No. of teasers = lots of repetitive text!)
  4. Replace removed (HTML) code-bloat with category/section name instead

The taxonomy_context module currently goes part-way to achieving our goals but doesn't address the last two steps and furthermore, relies on the help block to display our description text above out teasers which, apart from not being semantically-correct, could be problematic in themes which utilise a show/hide help system with different style "decorations" (CivicSpace is one such theme).

NOTE #1: I have only used this against the above site structure. If you have a more complex schema, this may not work as intended but it is hoped that, at minimum, you can now easily customise it to your needs.
NOTE #2: Clean URL's is enabled and I have the pathauto module installed.
NOTE #3: Tested only with PHPTemplate theme engine (Drupal's default).

Instructions

Open-up your node.tpl.php file and right underneath the first opening php line...

<?php /* $Id: node.tpl.php,v 1.4.2.3 2005/11/30 21:06:47 robinmonks Exp $ */ ?>

... make-way to copy/paste our first code snippet:

  <?php if ( !$page && arg(0)=="taxonomy" || $is_front) /* NOT a node page but a tax or the home page*/ : ?>
  <?php
	  $categories = <a href="http://api.drupal.org/api/4.7/function/taxonomy_node_get_terms" title="API details">taxonomy_node_get_terms($node->nid)</a>;
	  foreach ($categories as $category) { /* Re-write the category links to avoid code bloat from long link titles (description ) */
	  $categorylink = <a href="http://api.drupal.org/api/4.7/function/l" title="API details">l</a>(t($category->name), 'taxonomy/term/' . $category->tid, array('title' =>$category->name), NULL, NULL, TRUE) ."\n";
	  }
  ?>
	<?php if (($id == 1) && !$is_front) /* Place term desc at the top(id)  but NOT on home page */ : ?>
	<?php print $category->description ?>
		<hr />
	<?php endif; ?>
  <?php endif; ?>
 

Then, (further down the code) find the following snippet which writes the link under each teaser entry which points to the category's pertinent index page (www.example.com/category/index.htm):

<p>Posted in <span class="taxonomy"><?php print $terms ?></span></p>

NOTE: Don't worry if the span element isn't there, it's the php print $terms bit we're after here.

Replace it:

  <?php if ($links): ?>
      <p><?php if ($terms): ?> <span class="taxonomy">Posted in <?php print $categorylink ?></span><?php endif; ?></p> 
  <?php endif; ?> 

Props to Nick Lewis for his wicked Drupal tips and tutorials; particularly the article which put me in the right direction:
Some Tips on Working with Drupal Taxonomy Terms.

Contributions/additions/subtractions/corrections encouraged.

I hope this goes most, if not all the way to addressing what I believe (from my time searching for this solution here) is both a common website need, and a current weakness in Drupal's default taxonomy module (and/or core).

If this works for you, your category index pages will be leaner, faster, more accessible and search-engine-friendly.

Drupal rocks - UNDERSTAND DAT!

Comments

TheWhippinpost’s picture

DO NOT USE CODE ABOVE!

Really sorry about my in-vain attempts to be ultra-smooth and helpful by trying to appropriately link-out to the API-documentation within the formatted code blocks... compounded by the (seemingly) lack of edit options... which makes me mad!

Start again (ahem!):

...

In furtherance of this thread: Display vocabulary description, which seeks a way to display a term's (category) description as the introduction text for each section, or category, within a site.

The goal:

  1. Site directory structure thus: www.example.com/category/index.htm and; www.example.com/category/sub-category/index.htm
  2. To display an introductory description for each category (term description) above all sub-page teasers
  3. Remove (HTML) code-bloat from the link (anchor) title attribute beneath each teaser (currently term description x No. of teasers)
  4. Replace removed (HTML) code-bloat with category/section name instead

The taxonomy_context module currently goes part-way to achieving our goals but doesn't address the last two steps and furthermore, relies on the help block to display our description text above out teasers which, apart from not being semantically-correct, could be problematic in themes which utilise a show/hide help system with different style "decorations" (CivicSpace is one such theme).

NOTE #1: I have only used this against the above site structure. If you have a more complex schema, this may not work as intended but it is hoped that, at minimum, you can now easily customise it to your needs.
NOTE #2: Clean URL's is enabled and I have the pathauto module installed.
NOTE #3: Tested only with PHPTemplate theme engine (Drupal's default).

Instructions

Open-up your node.tpl.php file and right underneath the first opening php line...

<?php /* $Id: node.tpl.php,v 1.4.2.3 2005/11/30 21:06:47 robinmonks Exp $ */ ?>

... make-way to copy/paste our first code snippet:

  <?php if ( !$page && arg(0)=="taxonomy" || $is_front) /* NOT a node page but a tax or the home page*/ : ?>
  <?php
	  $categories = taxonomy_node_get_terms($node->nid);
	  foreach ($categories as $category) { /* Re-write the category links to avoid code bloat from long link titles (description ) */
	  $categorylink = l(t($category->name), 'taxonomy/term/' . $category->tid, array('title' =>$category->name), NULL, NULL, TRUE) ."\n";
	  }
  ?>
	<?php if (($id == 1) && !$is_front) /* Place term desc at the top(id)  but NOT on home page */ : ?>
	<?php print $category->description ?>
		<hr />
	<?php endif; ?>
  <?php endif; ?>
 

Then, (further down the code) find the following snippet which writes the link under each teaser entry which points to the category's pertinent index page (www.example.com/category/index.htm):

  <p>Posted in <span class="taxonomy"> <?php print $terms ?></span></p>

NOTE: Don't worry if the span element isn't there, it's the php print $terms bit we're after here.

Replace it:

  <?php if ($links): ?>
      <p><?php if ($terms): ?> <span class="taxonomy">Posted in <?php print $categorylink ?></span><?php endif; ?></p> 
  <?php endif; ?> 

Props to Nick Lewis for his wicked Drupal tips and tutorials; particularly the article which put me in the right direction:
Some Tips on Working with Drupal Taxonomy Terms.

Contributions/additions/subtractions/corrections encouraged.

I hope this goes most, if not all the way to addressing what I believe (from my time searching for this solution here) is both a common website need, and a current weakness in Drupal's default taxonomy module (and/or core).

If this works for you, your category index pages will be leaner, faster, more accessible and search-engine-friendly.

Drupal rocks - UNDERSTAND DAT!

sepeck’s picture

You would add it as a child page here:
http://drupal.org/node/45471

We try and get information like this (long term solutions/fix's and stuff) into the handbook rather then living in the forums.

As a bonus, you get listed here: http://drupal.org/node/14205

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

TheWhippinpost’s picture

Thanks Steve - I did consider submitting a handbook page but for 2 reasons:

  1. Feedback - Just in case it's peculiar-only to my setup or there's a blind-spot I've not picked-up-on
  2. I'd want to be a bit more verbose and include both a bit more text and screenshot(s)... but Drupal guidelines are PNG images-only IIRC :(

I don't want to side-track this thread but if it's one pet-hate I have with the Drupal community, it's the blasted PNG and/or QT/MOV (movie file) file-types :p

(Nuff said - Please start any browser-war threads elsewhere, thanking you :D)

Anyway, figured I'd let it live in the wild first and come-back to it later for handbook consideration. I do have plans to submit other stuff into the handbook at some (near) future point anyway.

Minty’s picture

Using Drupal 4.7 with taxonomy module.
This was just what I needed. Worked first time, no problems.
Many thanks

TheWhippinpost’s picture

Oh good!

I'm glad it worked for you Minty and thanks for stopping-by to say so. Not enough people do it seems, which ultimately only serves in putting people off contributing.

Thanks again.

Mike

TheWhippinpost’s picture

When using the pathauto module, it generates a path-to-category (or term if you prefer) like thus:

  • pathauto/node/category-name

I don't understand what purpose this serves (if anyone knows, I'd be grateful to know too) but I figure it'd be a more complete solution to handle a page request that comes-in via this form of URL.

So, for the main body of script I submitted in my 2nd post above, replace with the below instead:

<?php if ( !$page && arg(0)=="taxonomy" || arg(0)=="pathauto" || $is_front) /* NOT a node page, but a tax, pathauto (url) or our home page*/ : ?>
  <?php
      $categories = taxonomy_node_get_terms($node->nid);
      foreach ($categories as $category) { /* Re-write the category links to avoid code bloat from long link titles (description ) */
      $categorylink = l(t($category->name), 'taxonomy/term/' . $category->tid, array('title' =>$category->name), NULL, NULL, TRUE) ."\n";
      }
  ?>
<?php if (($id == 1) && !$is_front) /* Place term desc at the top(id)  but NOT on home page */ : ?>
<?php print $category->description ?>
<hr />
<?php endif; ?>
  <?php endif; ?>

All I've done is add the additional check to the 1st line of code:
arg(0)=="pathauto"

TheWhippinpost’s picture

If you use clean URL's and pathauto in conjunction with the taxonomy_menu module, this modification will write the correct category links as aliased by the taxonomy_menu module, instead of the duplicate generated by pathauto... For example:

This will also work if, for example, you have specified that only certain content types use Taxonomy Menu, but others, don't.

USAGE:
Tested on pages that list teasers on an index page (Basically, as this thread has set-out to achieve)... BUT, only tested with a category/sub-category directory structure (as detailed above). However, I've included more verbose comments in the code which might help should you need to customise.

The truth is, I'm not sure how taxonomy_menu builds paths deeper than this so just give it a try - it won't break anything.

For each teaser listed, this will print the correct (aliased) URL to the category belonging to the page represented by the teaser (with the page name used as anchor text and title), enabling you to include a link similar to, for example:

'Posted in: Category'

... beneath each teaser.

Note:
This is just a workaround until/unless the taxonomy_menu module is ever updated to work correctly with pathauto.

Paste the following code into the top of your node.tpl file instead of the code in my previous post (above: Slight Modification )

<?php if ( !$page && arg(0)=="taxonomy" || arg(0)=="taxonomy_menu" || arg(0)=="pathauto" || $is_front) : ?>
<?php /* Use print $categorylink in template for correctly aliased category links */
  $categories = taxonomy_node_get_terms($node->nid);
	  foreach ($categories as $category) {
			if (arg(0)=='taxonomy_menu') { /* If we're on a tax_menu-generated url */
			/* tax_menu uses vocabulary first, then category and child-categories in its aliases */
			/* EG: taxonomy_menu/vocabularyID/parent-categoryID/child-categoryID */
			/* Here, we get the parent category of the last child category in the path */
			$catparents = taxonomy_get_parents($category->tid, $key='tid');
				foreach ($catparents as $catparent) { /* Build our category link, ready-aliased */
				$categorylink = l(t($category->name), 'taxonomy_menu/' . $category->vid . '/' . $catparent->tid . '/' . $category->tid, array('title' =>$category->name), NULL, NULL, TRUE, TRUE) . "\n";
				}
				} else { /* Use normal taxonomy-generated paths */
	  $categorylink = l(t($category->name), 'taxonomy/term/' . $category->tid, array('title' =>$category->name), NULL, NULL, TRUE, TRUE) . "\n";
			}
	  }
 ?>
<!-- Theme below to suit -->
<?php if (($id == 1) && !$is_front) /* Place term desc at the top(id)  but only if NOT on home page */ : ?>
<?php print $category->description ?>
<?php print $categorylink ?>
<hr />
<?php endif; ?>
<?php endif; ?>

Just whack <?php print $categorylink ?> wherever you want to have the link appear.

Hope that's clear and serves you well
Mike

talkingwires’s picture

Is there a way to implement this using blocks instead of editing the template?

TheWhippinpost’s picture

I'm a bit slow today - I can't visualise in my mind what you want and I'm suspecting - after running across another post you've just made
here: Blocks on Taxonomy Pages: Determining TID and Parent VID? - that you may be in fact, fishing for a solution not directly dealt with by this topic... feel free to correct me but in the meantime, I'll blast over to that thread and see if can help there.

Mike
------------------------------------------------------------------------------------------
A simple thanks to those that help, a price worth payng for future wealth.

jamin0’s picture

At line 1373 in modules/taxonomy/taxonomy.module I replaced

$output =  taxonomy_render_nodes(taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE));

with

$current = taxonomy_get_term($current->tid);
$output =  $current->description ." ". taxonomy_render_nodes(taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE));

Of course there is more you can do with this but there it is...
The downside is that you need to reapply this change after any future taxonomy module updates.

Hope this helps...
Jamin

solutionsphp’s picture

Hmm, this seems like it should be easier. Is there not just a simple call that can be made to a variable to display the Term Definition?

I tried Jamin's solution, which is interesting despite the requirement of hacking a core module, but it doesn't quite achieve what I need. On my taxonomy/term pages, I simply want to display the Term Definition directly under the page heading:

<h1 class="title"><?php print $title; ?></h1>
<h2 class="term-description">Term Description Here</h2>

Jamin's solution printed the Term Definition from the *parent* term:

Term 1 Archive Page => Term 1 Definition printed
Term 2 (child of Term 1) Archive Page => Term 1 Definition printed
etc.

I would like:

Term 1 Archive Page => Term 1 Definition printed
Term 2 (child of Term 1) Archive Page => Term 2 Definition printed
etc.

Also, with Jamin's solution the term is printed as part of the $content variable, so I'm unable to style it without tearing apart that variable.

I also looked at the other solutions in this thread, but they are applied to the node template, and I want to apply this to my taxonomy-term templates (plus, even when inputted in the node template, the code didn't work... perhaps not Drupal 5.2 compatible?)

Can't this be done with a few lines of code in a template?

My review of David Mercer's "Drupal: Creating Blogs, Forums, Portals and Community Websites"