[Drupal] Display separate views on different taxonomy pages (parents/children) using Panels/Views
I have come across the problem a couple of times now and have come across someone else looking for the same things on IRC where you want to have a different view for parent terms and child terms.
Although this probably can be done in Views, I am none the wiser on how to do it and neither were any of the participants on IRC. So my work around is to use Panels taking advantage of Ctools contexts.
This tutorial is written for Drupal 6.
First of all, we need to download and enable a few modules.
- Ctools - (Chaos Tools, Page Manager, Views Content Panes)
- Panels - (Panels)
- Views - (Views, Views UI)
Once the modules are enabled we can continue.
If you haven't already, set up your taxonomy vocabulary with terms (they don't have to be in a hierarchy but I assume most will).
Next we will create our panel pages for the 2 types of term pages. In the image above, we have different types of clothing, most of which have children terms. So we want one page for all the parent terms with the exception of Suits as it doesn't have any children and we want to show the products in that term, then another page for Suits and all the child terms.
Go to Administer > Site Building > Panels.
On this page, under Manage pages, click the Enable button next to Taxonomy term template. When the new page loads, press the Edit button next to Taxonomy term template.
On this page, click the Add a new variant link.
I will start with the parent terms panel page, so enter in an appropriate admin title and tick the Selection rules box. In the drop down box select Taxonomy: Term and press Add.
In the popup, hold Ctrl and click all the terms that you want this page to apply to and press Save.
Set a page layout type and then add some basic content so that you can test that it is working properly. You will also need to set the Title to %term:name to reflect the standard Drupal taxonomy pages.
Save the page.
Now when you visit one of the parent terms, you will get the custom content rather than the default taxonomy page.
And visiting a child page still displays the standard Drupal taxonomy text.
If this is working, you can repeat the process by adding another Panel variant but this time, under selection rules selected all the child terms rather than the parent terms. Alternatively you could clone the existing variant, then under Selection rules, change the flag to Reverse (NOT), which will also target all the child terms.
Once this is done, your child terms pages will also display some custom text that you defined.
Now that is all working, we can set up our Views.
For the parent terms, we want to display the child terms only, no products. So when settings up the new view, select Term rather than Node.
Add an argument and select Taxonomy: Parent term.
Change the argument to Provide default value then select Taxonomy Term ID from URL.
Set up the rest of the view as you see fit, apply filters etc. Make sure that you add some way of clicking through to the child terms. If using fields, add Taxonomy: Term and make sure the Link this field to its taxonomy term page option is checked.
Save the View.
Now create another view but this time select Node rather than Term. For arguments, select Term ID or Term ID with depth and select the Taxonomy Term ID from URL like before. Apply any filters, fields etc that you need.
Once you are finished creating your views, head back to Administer > Site Building > Panels and click Edit next to Taxonomy term template.
Select one of the variants on the left and go down to Content. Remove any existing custom block content unless you want to keep it and add a new bit of content. Select Views on the left then select the appropriate view for that variant.
On Select display, just click Continue. On the next page, in the drop box labeled Taxonomy: Term ID:, select Term ID of first term. Repeat for the other page variant.
Now when we visit the taxonomy term pages the correct view will show up depending on what term is being viewed.
Parent item shows the child items:
Children terms show the nodes (in this case the empty text of the view):
You could even change the view for showing nodes of the term, by changing the argument to Taxonomy Term ID (with depth) which will show all nodes for that term and all it's child nodes, which you could place below the node showing child terms.
Which would result in: