最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Different Limit number of post on different archive page

matteradmin9PV0评论

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 );
  }
Post a comment

comment list (0)

  1. No comments so far