我在一个页面上运行多个循环,在其中一个循环中,我需要在第一篇文章中添加一个类,这样我就可以抵消差额。如何从wp_query向第一篇文章添加一个类别?
<?php $my_query = new WP_Query('category_name=news&posts_per_page=3');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID ?>
<div class="post-container">
</div>
<?php endwhile; ?>
我需要将类添加到具有类"post-container"的主div容器中。
谢谢你,下面的修复非常完美,但如果我想把它用于普通循环而不是wp_query,我该怎么办?像这个
<?php query_posts(array('showposts' => 3, 'post__not_in'=>$do_not_duplicate));
if (have_posts()) : while (have_posts()) : the_post(); ?>
也许这会有所帮助:
$first = false;
if( $my_query->current_post == 0 && !is_paged() ) { $first = true; }
然后你的线路和你这样做:
<div class="post-container <?= ($first ? 'classname' : ''); ?>">
未测试语法错误。
所以试试这个:
<?php
$first = true;
query_posts(array('showposts' => 3, 'post__not_in'=>$do_not_duplicate));
if (have_posts()) {
while (have_posts()) {
the_post();
echo '<div class="post-container '.($first ? 'classname' : '').'"></div>';
if ($first) {
$first = false;
}
}
}