0

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.

1

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

0

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 );
  }
  • you mean in function.php I have to add it? – mkafiyan Sep 1 '16 at 8:31
  • No, replace the third line of the code you posted. Note that I don't know what template page this is, but assume it is archive.php. Or a custom one for this post type? – cjbj Sep 1 '16 at 8:34
  • I add ` $args = array( 'post_type' => 'Author','paged' => $paged,'posts_per_page' =>5 );` because this custom post type have separate html page. I gave each of page above line with different post per page, but still have problem. when I click on the second page of author it shows 404 page – mkafiyan Sep 1 '16 at 8:36
  • But you do get 5 and 20 posts on the respective archives? – cjbj Sep 1 '16 at 8:38
  • I need to load 20 post in archive book in each page and load 5 author in author archive.I add post per page 5 to archive-author.php and 20 to archive-book.php and set max read to 20. but dosen't work fine – mkafiyan Sep 1 '16 at 8:41

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.