0

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.

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. Sep 9, 2019 at 7:46

1 Answer 1

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.

Your Answer

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

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