最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Dynamic loading of Archive content based on custom taxonomy term name

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 2, 2020 at 21:57 JamesJames 1191 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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();.

发布评论

评论列表(0)

  1. 暂无评论