admin管理员组文章数量:1416313
I tried to create a category template apply for parent and child specify category. I has create a file name 'category-1.php , in this file I used the loop for get the list of post:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Show content of category post here.
<?php endwhile;?>
<?php endif; ?>
In the function file, I put this code:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if( 0 < $cat->category_parent )
$template = locate_template( 'category-1.php' );
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );
I get the posts of child category ok, but the post in parent category not show full ( current 10 posts ). Has anyone tell me why ? Thank you.
I tried to create a category template apply for parent and child specify category. I has create a file name 'category-1.php , in this file I used the loop for get the list of post:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Show content of category post here.
<?php endwhile;?>
<?php endif; ?>
In the function file, I put this code:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if( 0 < $cat->category_parent )
$template = locate_template( 'category-1.php' );
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );
I get the posts of child category ok, but the post in parent category not show full ( current 10 posts ). Has anyone tell me why ? Thank you.
Share Improve this question asked Sep 9, 2019 at 6:52 Andy ThomasAndy Thomas 1 1- The template used has nothing to do with which posts are displayed. Is your problem that the template is incorrect, or that the wrong posts are being displayed? Your question title suggests the former, but the rest of your question is discussing the latter. – Jacob Peattie Commented Sep 9, 2019 at 7:46
1 Answer
Reset to default 0This if
statement is preventing you to use category-1.php
template for the parent categories:
if( 0 < $cat->category_parent )
Parent (top level) categories always have a $cat->category_parent === 0
. Remove that statement and try again, to see if this is the result you want to achieve.
Other than that, I am not really sure I understood you well. If you are wondering why not all of your posts are displayed on your template, know that by default, posts_per_page
on global queries is limited to what is set in your Settings > Reading > Blog pages show at most in the dashboard.
本文标签: wp queryHow to show specify category template for both parent and child category
版权声明:本文标题:wp query - How to show specify category template for both parent and child category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745174481a2646169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论