我使用WP\\U查询设置了两个循环:$latest
和$popular
我正在本示例布局中设置:
Latest                   ;Popular
Example HTML Output
<div class="content">
<div class="posts latest">
<post1>
<post2>
</div>
<div class="posts popular">
<post10>
<post9>
<post8>
<post7>
</div>
<div class="ad-block">
<ad>
</div>
</div>
<div class="content">
<div class="posts latest">
<post3>
<post4>
</div>
<div class="posts popular">
<post6>
<post5>
<post4>
<post3>
</div>
<div class="ad-block">
<ad>
</div>
</div>
[...]
简单地说:the
Latests 和
Popular post Div相邻浮动,并每隔几个帖子分开,以容纳全宽广告块。
My Loop
<div class="content">
<div class="posts latest">
<?php
$args1 = array (
\'posts_per_page\' => 1000000,
\'order\' => \'DESC\',
\'orderby\' => \'date\'
);
$latest = new WP_Query( $args1 );
if ( $latest -> have_posts() ) :
$count = 0;
while ( $latest -> have_posts() ) : $latest -> the_post();
$count++;
if ( $count % 5 == 0 ) :
get_template_part( \'template\', \'post\' ); ?>
</div> <!--/posts-latest-->
<div class="posts popular">
<?php
$args2 = array (
\'posts_per_page\' => 10,
\'order\' => \'ASC\',
\'orderby\' => \'date\',
);
$popular = new WP_Query( $args2 );
if ( $popular -> have_posts() ) :
while ( $popular -> have_posts() ) : $popular -> the_post();
get_template_part( \'template\', \'post\' );
endwhile;
endif;
?>
</div> <!--/posts-popular-->
<div class="ad-block">
<?php get_template_part( \'template\', \'ad\' ); ?>
</div>
</div><!--/content-->
<div class="content">
<div class="posts latest">
<?php
else :
get_template_part( \'template\', \'post\' );
endif;
endwhile;
endif; wp_reset_postdata();
?>
</div> <!--/posts-latest-->
</div>
现在,这是设置,以便在
$latest
循环
$popular
添加循环和ad块。
我理解为什么这不起作用。。。因为每5次发布$popular
循环被称为重新开始,而不是按顺序继续。
有什么想法可以让这一切顺利进行吗?
最合适的回答,由SO网友:Howdy_McGee 整理而成
试试这个:我们在outter循环的顶部声明一个递增器,名为$popularLoop
为了跟踪我们经历了多少流行循环,我们假设我们至少会遇到1个。
在我们的内部循环(流行)中,我们需要设置每页要加载的帖子数量,乘以我们经历过的流行循环数量,这将得到我们的偏移量。在内环的末端,我们增加$popularLoop
因此,我们的偏移量保持一致:
<div class="content">
<div class="posts latest">
<?php
$args1 = array (
\'posts_per_page\' => 1000000,
\'order\' => \'DESC\',
\'orderby\' => \'date\'
);
$latest = new WP_Query( $args1 );
$popularLoop = 0;
if ( $latest -> have_posts() ) :
$count = 0;
while ( $latest -> have_posts() ) : $latest -> the_post();
$count++;
if ( $count % 5 == 0 ) :
get_template_part( \'template\', \'post\' ); ?>
</div> <!--/posts-latest-->
<div class="posts popular">
<?php
$popular_ppp = 10;
$popularOffset = $popular_ppp * $popularLoop;
$args2 = array (
\'posts_per_page\' => $popular_ppp,
\'offset\' => $popularOffset,
\'order\' => \'ASC\',
\'orderby\' => \'date\',
);
$popular = new WP_Query( $args2 );
if ( $popular -> have_posts() ) :
while ( $popular -> have_posts() ) : $popular -> the_post();
get_template_part( \'template\', \'post\' );
endwhile;
$popularLoop++;
endif;
?>
</div> <!--/posts-popular-->
<div class="ad-block">
<?php get_template_part( \'template\', \'ad\' ); ?>
</div>
</div><!--/content-->
<div class="content">
<div class="posts latest">
<?php
else :
get_template_part( \'template\', \'post\' );
endif;
endwhile;
endif; wp_reset_postdata();
?>
</div> <!--/posts-latest-->
</div>