简体   繁体   中英

How to display custom number of posts on archive page?

I would like to change numbrer of posts displayed only on my archive pages, kindly let me know how to that, thank you.

This is the code from my archive.php

<div id="content-a">

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<div class="post">
<div class="imgdiv"><a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image() ?>" width="250"></a></div>
<div class="p-heading"><h1><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content"><?php the_excerpt(); ?></div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12"><?php $totalcomments = get_comments_number(); echo $totalcomments; ?></div>
</div>

<?php endwhile; ?>

I want to have different number of posts on archive pages. thank you.

Add this code to your functions.php and change the no of post from 100 instead how much you want to display.

function wpsites_query( $query ) {
if ( $query->is_archive() && $query->is_main_query() && !is_admin() ) {
        $query->set( 'posts_per_page', 100 );
    }
}
add_action( 'pre_get_posts', 'wpsites_query' );

try this

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'archive');
        $query->set( 'posts_per_page', 100 );
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

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