Different Limit number of post on different archive page

I set post per page from setting>maximum post per page to 20. I have 2 different custom post types 'book' and 'author' for archives of each of them. I want to load different number of post in page. I want to load 20 book per page in book archive and 5 post in author archive in each page. I also use WP-PageNavi plugin.

Here is my code

?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array( 'post_type' = 'Author','paged' = $paged,'posts_per_page' =5 ); 
$loop = new WP_Query( $args );
while ( $loop-have_posts() ) : $loop-the_post(); ?
a href="?php the_permalink(); ?" class="writer-link col-md-12 col-sm-12 col-xs-12"

    div class="writer-row1 col-md-12 col-sm-12 col-xs-12"
        div class="col-md-2 col-sm-3 col-xs-12 image-right"
        ?php the_post_thumbnail('post-thumbnail',array('class' = 'img-responsive')); ?
        /div
    div class="col-md-10 col-xs-12 col-sm-9 col-xs-12 pull-right writer-content"
    h3?php the_title(); ?/h3
    h4?php the_field('auth-trans'); ?/h4  
    ?php if ( get_field('writer-bio') ) { 
        echo 'p'.get_field('writer-bio').'/p';} ?

            span.../span
    /div
/div
/a

   ?php endwhile; // End of the loop. ?           

    div class="wp-pagenavi row"
        div id="wp_page_numbers text-center col-sm-6 center-margin"
            ul
                li class="active_page text-center"?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' = $loop )); } ?/li
            /ul
        /div
   /div

No problem with book archive: it loads 20 posts. But I don't know how I can make author page to load just 5 post per page and after it has loaded 5 first posts it is going to next page.

Topic plugin-wp-pagenavi wp-query posts custom-post-types Wordpress

Category Web


add below code in functions.php file , here "event" is custom post type (change it as per your post type) , so here it will display 6 post on events list page , also you need to copy default archive.php file and copy and create new archive-event.php (replace event with your post type).

 function custom_type_archive_display($query) {
    if (is_post_type_archive('event')) {
         $query->set('posts_per_page',6);
         $query->set('orderby', 'date' );
         $query->set('order', 'DESC' );
        return;
    }     
}
add_action('pre_get_posts', 'custom_type_archive_display');

Hope this Helps :)

More detail how to list custom post on custom page refer this link Custom Posts on Different Pages


You will have to make the query arguments dependend on the type of archive you are generating (which WP already knows from the page slug). Luckily there is a test for that, called is_post_type_archive. In the beginning of your code you would include this:

if (is_post_type_archive('books')) {
  $args = array( 'post_type' => 'Books','paged' => $paged,'posts_per_page' =>20 );
  }
elseif (is_post_type_archive('author')) {
  $args = array( 'post_type' => 'Author','paged' => $paged,'posts_per_page' =>5 );
  }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.