admin管理员组文章数量:1392059
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 badges1 Answer
Reset to default 1You 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();
.
本文标签: Dynamic loading of Archive content based on custom taxonomy term name
版权声明:本文标题:Dynamic loading of Archive content based on custom taxonomy term name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744782176a2624780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论