1

I have some taxonomy terms like Computer and Accessories, Games, Furniture etc. which I am able to access as /camera-and-accessories, /furniture and so on. These taxonomy terms are associated with my Product content type.

A taxonomy page is always displaying default Product title and Body fields. But I want to customize this page so that it displays additional fields like field_product_price, field_product_photo from my Product content type. I was hoping for something like {{ content.parent }} but I don't think such a thing does not exist (yet!)

Template suggestion by Twig debug

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'taxonomy_term' -->
<!-- FILE NAME SUGGESTIONS:
   * taxonomy-term--1.html.twig
   * taxonomy-term--category.html.twig
   x taxonomy-term.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/custom/ezone/templates/taxonomy-term.html.twig' -->

According to the above output I first overrode default taxonomy-term.html.twig with taxonomy-term--category.html.twig. In taxonomy-term--category.html.twig I did not put anything but just "Hello World!" to check whether this file is being hit. After rebuilding cache the page renders this static text but surprisingly along with that the actual content also got rendered on the page, which I don't know why.

I deleted taxonomy-term--category.html.twig and copied taxonomy-term.html.twig from stable theme to my theme's templates folder. Removed all entries in that file and again tried the same thing with a static text block. Same thing happened again!

Somebody please help with these.
1. How to access fields of a content type on a taxonomy term page
2. Why overridden templates shows taxonomy content when custom template has nothing written in it.

I am sorry if above questions looks stupid or childish. I am a newbie to Drupal.

3

When you display a taxonomy term the output is produced by this View:

admin/structure/views/view/taxonomy_term

The taxonomy term itself is in the header

HEADER
Global: Rendered entity - Taxonomy term (Global: Rendered entity - Taxonomy term)

and uses the taxonomy term template shown in your question, where you have only access to the fields of the term, not the content.

The format of the content is configured in the main part of the View. Default is to display the content as Teaser:

FORMAT
Format:Unformatted list | Settings
Show:Content | Teaser

If you want to change the fields, you can customize the Teaser in UI:

admin/structure/types/manage/article/display/teaser

Or in this node template (this targets the View, so it would also work, if you changed the view mode):

node--view--taxonomy-term.html.twig

Or switch to a field based View and configure the fields inside of the View.

  • Thank you quick reply. I can create a view as page with a custom URL or as a block. But I don't know how to create a view which actually points to a tax. page like /taxonomy-term. I would be grateful if you please help me with a step by step process of doing this. – Niladri Sarkar Dec 5 '16 at 15:27
  • Don't create a view, use the preinstalled view "taxonomy-term". This view uses the route "taxonomy/term/%". This is the canonical path. But most times you use an alias, like "/furniture". You can assign this alias individually in each term or generate it with pathauto. For the view it doesn't matter, if the page is invoked by using the canonical path or the alias. – 4k4 Dec 5 '16 at 15:43
  • +1 Read your reply for 4 times and Boooom!! You are my life saver :) And I didn't have to do anything in the template as I am already using view--view--fields.html.twig for All Product page! Did I understand this correctly btw? – Niladri Sarkar Dec 5 '16 at 15:46
  • So I don't really need a separate template for this because it matches the one I already have for views (views--view--fields.html.twig). Is this correct? – Niladri Sarkar Dec 5 '16 at 15:53
  • Yes, if you choose the last option and switch the format to "Show: Fields". – 4k4 Dec 5 '16 at 17:00

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.