Drupal - How to show the parent and child taxonomy term?
Views can do this. Views is a UI for sql searches so you are setting the definition for the query that will be made of the content database.
Create a view to display a taxonomy term with a page with a path configured along this line
category/%
. The%
is a wildcard that will make the view respond to any value applied in the URL that matches this format.Set up your display and any filters desired.
Define a relationship (third column of view UI page) and set it to taxonomy term: parent. This tells the view to look at the parent field in each taxonomy term searched. (Note that Drupal includes a field for each term that identifies its parent, but not its siblings or children).
Next we tell the view to interpret the wildcard from the URL we used in the path for the view: Define a contextual filter (above the relationship selection) and set it to a taxonomy:termID. In the form, set the relationship to Parent in the drop down box. This tells View that the term being called in the URL will be applied as a filter for the 'parent' field of the taxonomy terms as they are searched.
Finally we must complete refining the contextual filter. You can tell the view what to do if it does not find an answer in that section of the form if you want, but we must tell it what to do if it does find solutions in the query. To do so check the validation check box. Set the Validator to Taxonomy term and select the vocabulary that applies to your hierarchy. Then select either Term ID or Term name converted to term ID as the Filter value type. If the value you are passing in the % is a term ID, then you are done. If the passed value is a term Name, then you may want to check the box to convert dashes to spaces. IF you have multiple word term names, the URL will have dashes, which do not match the actual spaces in the term name. This little correction makes them agree so a test succeeds.
Finally go to the preview area and enter the name or ID for a term you know has children and you should get the list of children terms. Use the sorting settings to arrange them in whatever order you need.
Good Luck!
I have archive this use view too, but in slightly different way compare Ashlar.
- Add Taxonomy term: Term ID in CONTEXTUAL FILTERS.
- Add Taxonomy term: Parent term in RELATIONSHIPS. (leave Require this relationship unchecked.)
- Add Taxonomy term: Parent term in filter section. select great than 0.
do not forget to filter your taxnonmy term in mycase Taxonomy vocabulary: Machine name (= Business Directory).
add Taxonomy term: Rendered Taxonomy term to your fields , select relationship to parent , check the Exclude from display.
In setting, under FORMAT section. In grouping field 1, select (Parent) Taxonomy term: Rendered Taxonomy term.
You should good to go. you can use view tpl to add more class base on the group. I have enclose my screenshot for references. Sorry for my English.