我有一个循环与计数,以添加一个明确后,4项。这是相当好的工作,但有一个重铃铛排,将只需要一个员额。
代码:
Funders
'institution', )
);
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 4 == 0) :
echo $counter > 0 ? "" : ""; // close div if it's not the first
echo "";
endif;
$colors = get_field('institution_status');
if( $colors && in_array('funding', $colors) ){ ?>
ID) ?>前4个帖子在div中正确显示,下一个div只显示一个帖子,然后再显示两个具有正确计数的div。
这是.?
发布于 2019-07-23 21:51:09
对于您的清除包装器,您做错了。你有这个:
$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 4 == 0) :
echo $counter > 0 ? "" : ""; // close div if it's not the first
echo "";
endif;
// BUILDS INTERNAL DIVS
$counter++;
endwhile;所以你打开数字0的div和数字4的倍数。然后在这个语句中,只有当$counter大于0时,才关闭div。所以你最终关闭了div,然后打开一个可能不会关闭的新的div。你需要做两件事。首先,将关闭的div移动到while语句末尾的回显。其次,将结束的div包装在不同的条件中。现将其更正如下:$counter = 0;
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 4 == 0) {
echo "";
}
// BUILDS INTERNAL DIVS
/**
* Closing div for loop
*
* $counter is more than 0 AND $counter plus 1 is multiple of 4
* OR $counter plus 1 is the total posts found (the plus 1 here is because we start with 0 which counts toward our total)
*/
if ( ($counter > 0 && ($counter+1) % 4 == 0 ) || ($counter+1) == $loop->found_posts ) {
echo "";
}
$counter++;
endwhile;发布于 2019-07-23 20:34:54
你为什么不直接:
.author-nucleus:nth-of-type(4):after {
clear:both;
display: table;
content: "";
}在我看来,这就是你所需要的。css的真正美!不需要用打开和关闭div来计数。
解释:这个css-规则将在四个行的/div之后添加一个伪元素,这将清除浮点数。专业:轻量级,易变的Cons: /
https://wordpress.stackexchange.com/questions/343526
复制相似问题