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' a

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.

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.

Share Improve this question edited Sep 1, 2016 at 8:32 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Sep 1, 2016 at 7:11 mkafiyanmkafiyan 1513 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

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 );
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745603310a4635529.html

相关推荐

  • 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' a

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信