WordPress-WP_查询不加载指定数量的帖子
在我的页面上,我通过WordPress-WP_查询不加载指定数量的帖子,wordpress,random,posts,Wordpress,Random,Posts,在我的页面上,我通过newwp\u Query随机选择了一些帖子。问题是,“每页帖子”属性不起作用。这是我的密码: <div id="featured"> <?php $args = array( 'post_type' => 'post', 'orderby' => 'rand', 'posts_per_page' => 4, 'n
newwp\u Query
随机选择了一些帖子。问题是,“每页帖子”
属性不起作用。这是我的密码:
<div id="featured">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 4,
'nopaging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<div style="table full">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="featcell" style="background: url(<?php the_post_thumbnail_url(); ?>) center center">
<a class="featartlink" href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
</div>
<?php
}
echo '</div>';
wp_reset_postdata();
}
?>
</div>
我不知道为什么,但经过一些测试后,我尝试使用get_posts()
函数,现在一切正常。我只是想知道为什么新的WP\u查询不想工作
下面是使用get\u posts()
函数的正确代码
<div id="featured">
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'rand',
);
$rand_posts = get_posts( $args );
if ( $rand_posts ) :
echo '<div style="table full">';
foreach ( $rand_posts as $post ) : setup_postdata( $post ); ?>
<div class="featcell" style="background: url(<?php the_post_thumbnail_url(); ?>) center center">
<a class="featartlink" href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
</div>
<?php endforeach; wp_reset_postdata();
echo '</div>';
endif;
?>
</div>