1

Context

I've created my own custom taxonomy called kernal-category using the CPT UI plugin. This taxonomy is hierarchical and will have a large number of secondary, and tertiary categories.

Currently, to load the correct posts linked to the taxonomy terms I have created individual content-child pages for each term.

Example; the term news has a page of taxonomy-kernal_category-news.php

These are called from my archive.php page using the standard get_template_part( 'content-child', get_post_format() );

Each of these pages are identical in layout and design apart from the header on each page which should match the taxonomy term.

Problem

I'm going to be creating a lot of terms and that would lead to a lot of individual, duplicate pages which feels unnecessary.

Is there a way I can use a single template and somehow take the taxonomy term from the url, use it as a variable and then load the same page with my header text based on the variable? Or obviously a smarter, alternative.

1 Answer 1

1

You should only need one template. What you're describing is how things should work, if your templates are written correctly.

In your case you only need taxonomy-kernal_category.php. This template will be used for all terms automatically, and the correct posts will be displayed as long as you use the standard loop:

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        // Display post content
    endwhile; 
endif; 
?>

So there should be no manual querying of posts with WP_Query or anything like that.

To output the name of the current term dynamically, you can use the_archive_title(), and the description with the_archive_description().

the_archive_title() often prefixes the title with the taxonomy name, like "Kernal Category: News", so if you don't want that yuo can use single_term_title();.

1
  • 1
    Perfect answer, worked exactly as desired. Thanks.
    – James
    Commented Feb 3, 2020 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.