1

I have used this function to change the number of posts per page on a category page, but I can't get it to work for a sub-category page.

I'm trying to show 6 posts on the sub-category page and 12 on the parent category page.

function my_post_queries( $query ) {
  if (!is_admin() && $query->is_main_query()){

    if(cat_is_ancestor_of( 7, get_query_var( 'cat' ) )  ){
      $query->set('posts_per_page', 6);
    }
      if(is_category( '7' )){
      $query->set('posts_per_page', 12);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

When I use cat_is_ancestor_of on the archive page to change the layout of the sub-category page, it works, but when I use it in that function it doesn't.

0

OK, how about this then? (Changed from first try - now tested.)

function my_post_queries( $query ) {

    if ( ! is_admin() && $query->is_main_query() ) {

        if ( is_category( 7 ) ) {

            $query->set( 'posts_per_page', 6 );

        }

        //works with pretty permalinks
        $cat_id = get_queried_object()->term_id;

        if ( cat_is_ancestor_of( 7, $cat_id ) ) {

            $query->set( 'posts_per_page', 12 );

        }

    }

}

Note: Still not sure about the purpose or effect of including return ;'s. Had no obvious effect on functionality, so have left them out.

0

There is one obvious difference between your code and the similar code in the Codex - the use of a "return;" after each "set." So conforming your code to the example would look like this:

function my_post_queries( $query ) {

   if ( ! is_admin() && $query->is_main_query() ) {
       if( cat_is_ancestor_of( 7, get_query_var( 'cat' ) ) ) {
          //drumroll
          $query->set('posts_per_page', 6);
          //ta-da!
          return;
       }

       if( is_category( 7 ) ) {
          $query->set('posts_per_page', 12);
          //encore!
          return;
       }
    }
}

add_action( 'pre_get_posts', 'my_post_queries' );

Not tested. I wish I could authoritatively explain to you what "return;" does here. Before engaging on a research adventure, I'd be interested in knowing whether it solves the problem for you.

  • I tried it with the Codex code but still can't change the number of posts on the subcategory page. I'm realizing that I read the conditional explanation incorrectly though. It's targeting the parent category not the subcategory. I can't find how to target the subcategories. Codex says: is_category( 5 ) || cat_is_ancestor_of( 5, get_query_var( 'cat' ) ) Returns true when the category of posts being displayed is either term_id 5, or an ancestor of term_id 5 (subcategory, sub-subcategory...). – BethA Jan 19 '17 at 3:49
  • Well... that still leaves open the question about the function (if any) of the return;, Will look into this in more detail shortly if you or someone else hasn't figured it out first. – CK MacLeod Jan 19 '17 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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