[英]Wordpress - Limiting Number of Posts
How can I adapt this code to limit the number of posts to say 3 on the page? 如何调整此代码,以限制页面上说3的帖子数? It's taken from a Wordpress template.
它取自Wordpress模板。 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: 设置posts_per_page参数:
$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 有关详细信息,请参见http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
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. 上面的代码将处理的最大元素数限制为3。只需更改while部分,它就会运行。 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.
无论如何,正确的方法是have_post()方法可以选择限制检索到的元素数量,然后仅显示该元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.