简体   繁体   中英

wordpress limit number of posts per category

how can I limit the number of posts per category in a Wordpress custom post type?

I'm making a portfolio site for a client with my own Wordpress theme. And on the homepage there are only 8 spots for "Featured" projects. When you create / update a project, you can check the category Featured. How can I make sure the category featured can't be checked more than 8 times.

you can use a function like this one

add_action('pre_get_posts', 'myposts_limit');
function myposts_limit( $query ) { 
  if( !is_admin() && is_tax( 'featured' )) { //change featured with your custom taxonomy
     $query->set( 'posts_per_page', 10); //10 posts on your custom taxonomy
   }        
}

If you want to limit more check the below function:

function myposts_limit( $query ) {
 if( $query->is_category() && !is_admin()):
    $query->set('posts_per_page', 14); //14 posts on category
   return;
 endif;
 if( $query->is_search() && !is_admin()):
    $query->set('posts_per_page', 20); //20 posts on search
   return;
 endif; 
 if( $query->is_tag() && !is_admin()):
    $query->set('posts_per_page', 30); //30 posts on tag template
   return;
 endif;  
 if( !is_admin() && is_tax( 'featured' )) { //change featured with your custom taxonomy
      $query->set( 'posts_per_page', 10); // 10 posts on your custom taxonomy
  }          
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM