在我的主页上,我有6个街区,包括每个街区。
我想随机展示这6个街区。
我的代码看起来像这样:
<div class="bloc">
<h2><?php the_field('titre1'); ?></h2>
<div class="content">
<p><?php the_field('description1'); ?></p>
</div>
<p class="more"><a href="<?php the_field('url1'); ?>">read more</a></p>
</div>
<div class="bloc">
<h2><?php the_field('titre2'); ?></h2>
<div class="content">
<p><?php the_field('description2'); ?></p>
</div>
<p class="more"><a href="<?php the_field('url2'); ?>">read more</a></p>
</div>
<div class="bloc">
<h2><?php the_field('titre3'); ?></h2>
<div class="content">
<p><?php the_field('description3'); ?></p>
</div>
<p class="more"><a href="<?php the_field('url3'); ?>">read more</a></p>
</div>
etc... (6 times)
你知道是否有办法随机化这个?
我事先感谢你的帮助!
答案 0 :(得分:0)
将每个块和push
作为array
添加到php string
中。然后通过它们调用shuffle($array)
然后foreach
来输出它们。
如果您需要更具体的说明,请与我们联系。编辑:完整说明;
<?php
// init array
$blocks = array();
// loop through 1 to 6
for($i=1;$i<=6;$i++) {
// create a block of html
// note I use $i here to get the correct fields
// also note I've used get_field so its not echoed (as opposed to the_field)
$block = '<div class="bloc">
<h2>'.get_field('titre'.$i, 'option').'</h2>
<div class="content">
<p>'.get_field('description'.$i, 'option').'</p>
</div>
<p class="more"><a href="'.get_field('url'.$i, 'option').'">read more</a></p>
</div>';
// push this block of html into the array
array_push($blocks, $block);
}
// shuffle the array of blocks randomly
shuffle($blocks);
// loop through them and echo out each html block
foreach ($blocks as $index => $block) {
echo $block
} ?>
祝你好运!
编辑:抱歉,输入的速度非常快,并犯了一些错误。