How can I adapt this code to limit the number of posts to say 3 on the page? It's taken from a Wordpress template. Thanks in advance for any help!
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged
);
$alt_posts = new WP_Query($alt_args);
?>
<?php if ( $alt_posts->have_posts() ) : ?>
<?php while ( $alt_posts->have_posts() ) : $alt_posts->the_post(); ?>
<?php get_template_part( 'content', 'alt-homepage' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'wp-jurist' ); ?></h1>
set the posts_per_page parameter:
$alt_args = array(
'ignore_sticky_posts' => 1,
'paged' => $paged,
'posts_per_page' => 3
);
see http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters for details
While is not the right thing to do, this will work:
<?php
$i = 0;
while ( $alt_posts->have_posts() && $i < 3 ) {
$i++;
$alt_posts->the_post();
get_template_part( 'content', 'alt-homepage' );
} ?>
The code above will limit the maximum of proccessed elements to 3. Just change the while part, and it'll run. Anyway, the correctness would be that the have_post() method would have an option to limit the number of elements retrieved, and then only show that elements.
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.