wp query - How to show specify category template for both parent and child category

I tried to create a category template apply for parent and child specify category.I has create a file name 'catego

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
Add a comment  | 

1 Answer 1

Reset to default 0

This 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745174481a4615122.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信