我有一个查询,它从所有类别中提取帖子,除了一个ID为2的类别。在第一个循环中,我有一个if语句来计算偶数和奇数帖子,以改变我的html(工作正常)。
在每2篇文章的末尾插入一个div(我可以这样做),在该div中,我需要从ID为2的类别中随机查询一篇文章。
很难理解如何在循环中循环。我见过一些不同的例子,但似乎没有一个是有效的。
这是我的代码精简了一点。
<?php
$counter = 0;
query_posts(\'cat=-2&orderby=date&order=DESC\'); while (have_posts()): the_post(); $counter++; ?>
<?php if( $counter % 2 == 0 ) : ?>
<!-- Displaying even posts here -->
<?php else: ?>
<!-- Displaying odd posts here -->
<?php endif; ?>
<?php if( $wp_query->current_post == 1 ) { ?>
<div class="post__splitter">
<!-- This is where a need to query a random post from the category with an ID of 2. -->
<!-- query_posts(\'cat=2&orderby=rand&post_per_page=-1\') -->
</div>
<?php } ?>
<?php endwhile; ?>
编辑:
这是我到目前为止的全部代码。。。遇到了显示所有帖子的问题,加上大约1000万个帖子。。我想不出来。。。
<?php
$counter = 0;
$outer_query = new WP_Query( array( \'cat\' => \'-2\', \'orderby\' => \'date\', \'order\' => \'DESC\' ) ); while ($outer_query->have_posts()): the_post(); ?>
<?php if( $counter % 2 == 0 ) : ?>
<div class="post">
<div class="post__thumbnail"><?php the_post_thumbnail(); ?></div>
<div class="post__content_wrapper">
<div class="post__content">
<div class="post__title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
<div class="post__caption"><?php the_excerpt(); ?></div>
<div class="post__details">
<a class="post__date" href="<?php the_permalink();?>"><?php the_date(\'F j, Y\', \'Posted on \'); ?></a>
<?php the_tags(\'<span class="post__tags">Tagged with: \',\', \',\'</span>\'); ?>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="post post--odd">
<div class="post__content_wrapper">
<div class="post__content">
<div class="post__title"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
<div class="post__caption"><?php the_excerpt(); ?></div>
<div class="post__details">
<a class="post__date" href="<?php the_permalink();?>"><?php the_date(\'F j, Y\', \'Posted on \'); ?></a>
<?php the_tags(\'<span class="post__tags">Tagged with: \',\', \',\'</span>\'); ?>
</div>
</div>
</div>
<div class="post__thumbnail"><?php the_post_thumbnail(); ?></div>
</div>
<?php endif; ?>
<?php if( $wp_query->current_post == 1 ) { ?>
<div class="post__splitter">
<?php
$inner_query = new WP_Query(array(\'cat\' => 2, \'orderby\' => \'rand\'));
the_content();
?>
</div>
<?php } ?>
<?php endwhile; ?>
Final Working Edit
<?php
$counter = 0;
$args = array (
\'order\' => \'DESC\',
\'orderby\'=> \'date\',
\'cat\' => \'-2\',
);
// The Query
$outer_query = new WP_Query( $args ); if ( $outer_query->have_posts() ) : while ( $outer_query->have_posts() ) : $outer_query->the_post(); $counter++; ?>
<?php if( $counter % 2 == 0 ) : ?>
// even posts
<?php else: ?>
// odd posts
<?php endif; ?>
<?php if( $outer_query->current_post ==1 ) { ?>
<?php $args2 = array(
\'cat\' => \'2\',
\'posts_per_page\' => \'1\',
\'orderby\' => \'rand\',
);
$inner_query = new WP_Query( $args2 ); if ( $inner_query->have_posts() ) : while ( $inner_query->have_posts() ) : $inner_query->the_post(); ?>
// inner query pulling random post.
<?php endwhile; endif; ?>
<?php } ?>
<?php endwhile; endif; ?>