Drupal 8:在父分类术语展示位置显示子分类术语
Hey there! I totally get what you’re aiming for—showing a parent taxonomy term’s full fields alongside its child terms creates a much more intuitive, connected page experience. Since you mentioned entity references and taxonomy terms, I’m assuming you’re working with Drupal (feel free to correct me if you’re using a different CMS!), and here are a few solid approaches to make this happen:
1. Embed a View (No-Code, Fast Setup)
This is the easiest way if you don’t want to mess with code:
- Create a new View that targets Taxonomy Terms.
- Add a Contextual Filter for "Taxonomy term: Parent term", set it to use the current term’s ID from the URL (or from the entity being viewed).
- Configure the View’s display (unordered list, grid, teaser cards—whatever fits your design) to show the child terms’ fields you want.
- Head back to your parent term’s Manage Display settings, add a new "View" field, select the View you just built, and place it wherever you want in the field order (e.g., right after the parent term’s description).
2. Custom Field Formatter (Clean, Scalable)
If you want a more integrated solution, build a custom field formatter (or use a contributed one if available):
- For Drupal 8/9/10, you can create a small custom module to define a new formatter for the taxonomy term entity type.
- In the formatter’s
build()method, load the parent term’s children using\Drupal\taxonomy\TermStorageInterface::loadChildren($parent_term_id). - Use the entity view builder to render the child terms (e.g.,
\Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')->viewMultiple($child_terms, 'teaser')), then attach that render array to the parent term’s field output. - Enable the module, then select this new formatter in your parent term’s display settings.
3. Override the Twig Template (Full Control)
If you’re comfortable with Twig theming, this gives you total control over the layout:
- Override the default taxonomy term template (usually
taxonomy-term.html.twig) for your specific vocabulary (copy it to your theme’s templates folder and rename it liketaxonomy-term--my-vocabulary.html.twig). - Add code to load and render child terms directly in the template:
{# Load child terms for the current parent term #} {% set child_terms = taxonomy_term_load_multiple_children(tid) %} {% if child_terms %} <section class="child-terms"> <h2>Subcategories</h2> <div class="child-terms-grid"> {% for term in child_terms %} {# Render each child term with its full fields using the 'full' view mode #} {{ entity_view(term, 'full') }} {% endfor %} </div> </section> {% endif %} - Adjust the markup and view mode to match your site’s design.
4. Reverse Entity Reference Field (Core-Only)
If your vocabulary uses a parent reference field (standard in Drupal), you can set up a reverse reference:
- Go to your vocabulary’s Manage Fields page, add a new Entity Reference field.
- Set the target type to "Taxonomy term", then under "Reference type", choose "Reverse reference" and select the parent field from your vocabulary.
- Save the field, then add it to your parent term’s Manage Display settings. You can format it as a list, grid, or any other available formatter to show the child terms.
Pick the method that fits your technical comfort level and project needs—all of these will let you display parent term fields and child terms together seamlessly.
内容的提问来源于stack exchange,提问作者Ben

