I know that if one knows how to write php code, then you can put a snippet in the header of your taxonomy view to display the related taxonomy description. I found this snippet of code for 6.x-3.0, but it doesn't work for 7.x.

<?php
$tid = arg(2);
$term = taxonomy_get_term($tid);
if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
  $description = i18nstrings("taxonomy:term:$term->tid:description", $term->description);
}
else {
  $description = $term->description;
}
print $description;
?>

Can anyone help me adapt this for Drupal 7?

Or does anyone know of a different way to display the taxonomy's description at the top the that particular term's page?

Comments

traceelements’s picture

Upon further inspection of Drupal 7, I see that I can add fields to each taxonomy term, such as a image field, as well as a description field. How do I get all of those fields to display in a custom taxonomy view?

dawehner’s picture

Did you find out already which part of the code does not work?

* Is $term loaded?
* Is $term->description not set?

traceelements’s picture

I get this error:

Fatal error: Call to undefined function taxonomy_get_term() in /xxx/modules/php/php.module(75) : eval()'d code on line 3

dawehner’s picture

Status: Active » Fixed

In d7 you have to use taxonomy_term_load($tid)

traceelements’s picture

Thank you, dereine. I'm not getting the error anymore, but the description still won't show up in my view. Any ideas?

traceelements’s picture

Status: Fixed » Active
Andrey Zakharov’s picture

$view = views_get_current_view();
$tid = $view->args[0];
 
if ( is_numeric( $tid ) ) {
  $term = taxonomy_term_load($tid);
 
  if (module_exists('i18n_taxonomy') && i18n_taxonomy_vocabulary_mode($term->vid) == I18N_MODE_LOCALIZE) {
    $description = i18n_string("taxonomy:term:$term->tid:description", $term->description);
  } else {
    $description = $term->description;
  }
  print $description;
}

works for me

dawehner’s picture

Status: Active » Fixed

Okay try this code.

Damani’s picture

Title: How to put taxonomy description into taxonomy views page? » How to put content related by taxonomy into taxonomy views block?
Component: Miscellaneous » taxonomy data
Status: Fixed » Needs review

i am trying to set up a block to display related content using drupal 7. So depending on the node you are viewing it should display similar content related to that node by taxonomy, similar to how youtube has it when viewing a video.

currently i tried to set the default argument by inserting exactly this code:

$node=node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){$terms[]=$term->tid;}
return implode('+',$terms);
}else {return;}

but alas the block does not show up. instead i get the following error:
Notice: Undefined property: stdClass::$taxonomy in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: Invalid argument supplied for foreach() in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Notice: Undefined variable: terms in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: implode() [function.implode]: Invalid arguments passed in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).

dagmar’s picture

Title: How to put content related by taxonomy into taxonomy views block? » How to put taxonomy description into taxonomy views page?
Component: taxonomy data » Miscellaneous
Status: Needs review » Fixed

Damani has already posted other issue for his problem #1058674: Only works with first taxonomy term views3 D7. Reverting status of this issue.

Damani’s picture

The title change was by mistake. Sorry my bad.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

borazslo’s picture

thx!

jon nunan’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev

I updated my dev version after the weekend and now my term description fields generate 'notices' and are output as plain text. Anyone else getting this?

I added a textarea field to my taxonomy term in which I copied the description into. I then removed the description field from my view and it is now displaying fine. So I think the problem is just with 'term description'.

The notices generated when displaying the 'description' field is:

Notice: Undefined index: taxonomy_term_data_field_data_field_category_format in views_handler_field->get_value() (line 738 of <WEBSITE-DIRECTORY>\httpdocs\sites\all\modules\views\handlers\views_handler_field.inc).

The taxonomy is tied to the content through a Term Reference field called 'category' and it was working fine in earlier builds this month.

elgandoz’s picture

subs

rt_davies’s picture

I see that I can add fields to each taxonomy term, such as an image field, as well as a description field. How do I get all of those fields to display in a custom taxonomy view?

  1. Edit your taxonomy vocabulary.
    1. Add the field (or fields) that you want. (Hint: The existing field named "Description" doesn't work, so create a new text-area field if that's what you want. I called my replacement field "Definition".)
    2. From the List tab, edit each term's value for the new field.
  2. Create a new view.
    1. Show: Taxonomy terms
    2. Of type: your_vocabulary, with a page display
    3. Continue & Edit
    4. Configure the view to display only your new field(s).
    5. Use pager: Display a specified number of items | 1 item
    6. Add a Taxonomy Term contextual filter
  3. Edit the existing Taxonomy Term view.
    1. Click on the "add" button in the Header section
    2. Select Global view type header
    3. Select the new view you created above
    4. Check the "Inherit contextual filters" checkbox

Now when you visit the Taxonomy Term view, your new fields view will be included at the top.

rocbrook’s picture

rt_davies' solution worked like a charm. I didn't need to create a separate description field as it showed OK for me.

However, this seems like it should work right out of the box, the fields show fine on the default term display so why don't they show in the view override?!

Thanks rt_davies!

mrP’s picture

#7 works like a charm as well for a straight term description implementation.

@Andrey Zakharov - thank you kindly

jaypan’s picture

#16 worked for me, but this point was a little unclear:

6. Add a Taxonomy Term contextual filter

The contextual filter needs to be on the term ID.

lolandese’s picture

#16 works now (D7.12) without creating a new text-area field. You can skip the first section. Thanks.

Azamat’s picture

Thank you !

sahuni’s picture

Big Thanks, it works also for me now.

Just a problem with multilingual website
I use the default term description, I've translated it.
taxonomy views is filtered by language, but my description stay in my default language.
I tried in new view to add a filter by language, the only effect is that description disappears.
Any idea?

stephane bouillet’s picture

Hie,

#16 also works for me (D7.14): Thank you very much !! :)

p55mac’s picture

#16 Also worked for me.

But I had trouble with

6. Add a Taxonomy Term contextual filter

as well. It came up saying

An illegal choice has been detected. Please contact the site administrator.

Whenever I tried to add a Taxonomy term: Term ID contextual filter. In the end I just pressed cancel and it left the filter in place... but it worked regardless.

Also, I could use the the existing/default field named "Description".

trupal’s picture

works great on taxonomy view but how to display it in catalog view?

jwilson3’s picture

UPDATE: see comment #28 below for the best D7 solution.
Here is an improved version of the solution in #7 that will respect the *text format* of the term description field:

$tid = arg(2);
$term = taxonomy_term_load($tid);
if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
  $description = i18nstrings("taxonomy:term:$term->tid:description", $term->description);
}
else {
  $description = $term->description;
}
print check_markup($description, $term->format, '', TRUE);
gratefulsk’s picture

Adding this php code to the header worked for me

<?php $current = taxonomy_term_load(arg(2)); ?>
<?php if ($current): ?>
    <div class="taxonomy-description">
        <?php echo $current->description; ?>
    </div>
<?php endif; ?>
dennisg’s picture

#26 didn't work for me on Drupal 7.
But this one worked:

$tid = arg(2);
$term = taxonomy_term_load($tid);
if (module_exists('i18n_taxonomy') && i18n_taxonomy_vocabulary_mode($term->vid) == 1) {
  $description = i18n_string("taxonomy:term:$term->tid:description", $term->description);
}
else {
  $description = $term->description;
}
print check_markup($description, $term->format, '', TRUE);

i18n_taxonomy_vocabulary_mode($term->vid) == 1 means that you have selected Localize translation mode on edit vocabulary page.

jwilson3’s picture

Good call on #28, i didn't test the i18n integration in #26. Thanks.

betarobot’s picture

Thanks @celticremark, #27 worked just lovely for me!

bendev’s picture

#28
thanks !

danny englander’s picture

This was a trivial matter of adding a relationship "Add a relationship "Content: Taxonomy terms on node" and then adding in the Replacement pattern for Taxonomy term: Term description field into a global text header in the view ([description_field]).

stoan’s picture

@highrockmedia may you elaborate more, I'm try your solution, I'm don't understand what I'm suppose to do from here "then adding in the Replacement pattern for Taxonomy term: Term description field into a global text header in the view ([description_field])"
I have added the relationship on the taxonomy view. Thanks

Thanks
Siya

kaynen’s picture

@stoan I believe highrockmedia means that you should add a "taxonomy terms: term description" field to the view, exclude it from display, and then add a "global: text area" to the header with the replacement pattern [description]. Let me know if that helps!

mrpauldriver’s picture

@highrockmedia I am not sure that it is quite so easy if your view does not contain fields - when displaying rendered entities for example.

I don't code, but had a go at #26 #27 & #28. I just ended up with broken php showing in my header - not sure what I was doing wrong.

The post at #16 contains a good explanation of the issues and got me sorted out.

It does seem odd however, that the default taxonomy field has a bug that prevents its' display in a taxonomy view??

danny englander’s picture

#33 and #35, it's a long time ago but I believe I added a description as a new field within my vocabulary and then that was available with views and all the goodness that comes with it.

shamerlinck’s picture

This was a valid solution for what I was after. Just wanted to give a referral where it's due.
rt_davies' #16 worked here.

Similarly, as mentioned elsewhere on the thread, #6 isn't spelled out all the way. If you need it a little more descriptive, I used:

Contextual Filter:
Taxonomy term: Term ID
On configuration of the filter:
Clicked the 'Set Default Value' button,
and set it to 'Taxonomy term ID from URL'
and checked 'Load default filter from term page'

That did the trick for me.

firesidelibrarian’s picture

I have been searching for a way to do this for hours, and #16 + #19 worked like a charm for me. Thanks so much!

ProYoga.NL’s picture

Issue summary: View changes

Tried #16, #19 and #37, but eventually gave up after many failed attempts. Remaining problem with my configuration, view would only take the first taxonomy term description from all the content nodes that shared the same vocabulary or create multiple duplicates, instead of selecting only the description of the selected taxonomy term.

The following links were helpful to understand the required steps (and possible shortcuts), but didn't provide the final solution:
http://pixelheadstudio.com/blog/solved-drupal-7-missing-taxonomy-term-de...
http://sergiy.kyrylkov.name/2012/02/29/showing-taxonomy-description-in-t...

In my case it was #28 that provided the fix I was looking for. Plus enabling Query Settings > Distinct. Thanks for sharing!

Anonymous’s picture

Thank you, rt_davies! This was EXACTLY what I needed for a project.

dries arnolds’s picture

Following the method in #32 I couldn't get rid of duplicates with distinct only and both distinct and pure distinct.

Then I tried #16 which works very well. Only difference is that I used a block display, since I didn't need the term fields to be accessible by users/search engines apart from the taxonomy/term listing.

aamato’s picture

I can't believe, I still have the same problem. And this bug wasn't solved yet. I can only find workarounds, no definitive solution...

bill.zero’s picture

In case you haven't found other threads about PHP code in Views headers in D7, you need two more things:
1) Enable PHP Filter module;
2) Change the Text Format of the Global:Text Area to "PHP Code".

#27 worked for me.

selfirian’s picture

#27 worked for me as well

designSHIFT’s picture

Here is an export of the view that worked for me:
$view = new view();
$view->name = 'taxonomy_header_desc';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'Taxonomy Header & Desc';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '1';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_taxonomy'] = TRUE;
/* Field: Taxonomy term: Term description */
$handler->display->display_options['fields']['description']['id'] = 'description';
$handler->display->display_options['fields']['description']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['description']['field'] = 'description';
$handler->display->display_options['fields']['description']['label'] = '';
$handler->display->display_options['fields']['description']['element_label_colon'] = FALSE;
/* Contextual filter: Taxonomy term: Term ID */
$handler->display->display_options['arguments']['tid']['id'] = 'tid';
$handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_term_data';
$handler->display->display_options['arguments']['tid']['field'] = 'tid';
$handler->display->display_options['arguments']['tid']['default_action'] = 'default';
$handler->display->display_options['arguments']['tid']['default_argument_type'] = 'taxonomy_tid';
$handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25';

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');

vorbian’s picture

Thank you rt_davies #16. Total genius. Not only did this help me for my current project to put my taxonomy description and image in the taxonomy view header, but it opened my eyes to a whole new way to add headers (which contain other views) to a current view. Awesomesauce!

giorgosk’s picture

if #1957266: Rendered entity in views header accepting arguments from URL gets committed things will be simpler

until then the simpler solution I found is
1. create view with dump from #45 comment (adjust to your liking)
2. go your ORIGINAL taxonomy view and add Header: "Global: View area"
choosing the view from 1. and check "Inherit contextual filters"

Emiliano’s picture

Thank you, rt_davies #16! It works perfectly!

buddym’s picture

Just adding a little note here for those that find this thread when searching for a fix on how to get your Taxonomy Terms to display the Description field (and other adhoc fields that can optionally/subsequently added to a Taxonomy vocabulary, i.e., Manage Fields/Display). The fix is to simply disable the "Taxonomy term" view and let Drupal handle the display like any other node.

rwilson0429’s picture

An old thread but a note to myself and others who come here looking for a solution to display a term's description (or any other term field) on the Taxonomy Term View page that ships w/Views. I'm not sure why in d7 others would find it necessary to write php code to display "taxonomy term description on the taxonomy views page". Neither do I agree with a solution that would require me to "disable the Taxonomy Term view and let Drupal handle the display like any other node".

The solution that seems to work for my installations are:

  • if your Taxonomy Term View is displaying content (i.e. Teasers) use #16
  • If your Taxonomy View is displaying fields, use #32 as further explained by #34,
bramvandenbulcke’s picture

I know this is a Drupal 7 thread but anyone looking for a solution for Drupal 8: solution #16 also works well on Drupal 8!

shane birley’s picture

Another method that people could try is by creating a block that displays the fields you want. After creating the block, you would display it at the top of the taxonomy terms page.

nelslynn’s picture

Nothing is working for me here and I believe it's because my taxonomy term page is 2 deep. For example, I'm trying to display the description on a term page structure like this: catalog/main-term/sub-term (catalog/%/%). I want the description of the last term in the URL to display before the nodes teasers associated with the sub-term.

I hope this makes sense. I've searched all over for a solution and come up empty. If anyone has some incite, I'm all ears. Thx