不管什么原因posts_per_page
参数不起作用。我已经在模板中调用了它wp_reset_query()
, 但它不适用于第二个实例。知道为什么吗?
<div class="new_home_single">
<ul>
<?php $the_query = new WP_Query( \'posts_per_page=6\' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="posting_classs">
<a href="<?php the_permalink()?>">
<?php
/***** Thumbnail ******/
the_post_thumbnail(
array(280, 150),
array(
\'class\' => \'thumbnail_class\', //custom class for post thumbnail if any
\'alt\' => \'post thumbnail\', //post thumbnail alternate title
\'title\' => \'my custom title\' //Title of thumbnail
)
);?></a>
<div id="post-<?php the_ID(); ?>" <?php post_class( \'post_box\' ); ?>>
<h2 class="title_home"><a href="<?php the_permalink() ?>">
<?php echo ShortenText(get_the_title()) ; ?></a></h2>
<img src= "<?php echo $imglink; ?>calendar.png" class="recent_post_date_img"><div class="date_wrap"><p class="date1"><?php the_date(\'m-d-Y\', \'\', \'\'); ?></p></div>
<div class="comment_wrap"> <?php comments_popup_link("$comm_link 0", "$comm_link1", "% $comm_link"); ?></div>
<div class="excerpt_class">
<?php echo the_excerpt(25); ?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
wp_reset_query();
?>
</ul>
</div>
</div>
<div class="sidebar1"><?php dynamic_sidebar( \'sidebar-1\' );?></div>
<div class="gap"></div>
<?php $the_query2 = new WP_Query( \'cat=10\', \'posts_per_page=1\' );
while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
<a href="<?php the_permalink()?>">
<div class="wrap_video">
<div class="play_button"> </div>
<?php
/***** Thumbnail ******/
the_post_thumbnail(
array(200, 200),
array(
\'class\' => \'video_class_thumb\', //custom class for post thumbnail if any
\'alt\' => \'post thumbnail\', //post thumbnail alternate title
\'title\' => \'my custom title\'
)
);?>
</div>
</a>
<?php endwhile;
wp_reset_query();?>
</div>
最合适的回答,由SO网友:Dave Romsey 整理而成
第二个查询的问题是由于参数的传递方式造成的。在原始代码中,传递了两个字符串,但这行不通:
$the_query2 = new WP_Query( \'cat=10\', \'posts_per_page=1\' );
可以使用数组(首选方法):
$the_query2 = new WP_Query( array(
\'cat\' => \'10\',
\'posts_per_page\' => \'1\'
) );
或使用查询字符串:
$the_query2 = new WP_Query( \'cat=10&posts_per_page=1\' );