继在stackoverflow上发布后,我能够修改我的代码,为我的div指定一个随机类。我现在面临的问题是,因为它是随机的,所以它与materializecss的部分工作。在php中指定随机类,但限制类可以归因的时间数
基本上,当我有5或6个职位来显示我想随机化的外观和各种列大小。只有两个选项(无论是6列宽还是3列)。每个“行”应该总共有12列。
这是我的问题:我怎样才能限制“col s12 m6 l6”类的给定时间?
下面的代码:
if($count == 5):
$the_class = array(
'col s12 m6 l3',
'col s12 m6 l6'
);
$class = $the_class[array_rand($the_class)];
endif;
然后我附和类:
<?php echo $class; ?>
当随机分配给3倍的“山坳S12 M6 L6”这是完美的,但是当它更或少于它不工作。 我知道array_rand可能不是实现这个目标的最好方法,但它对我来说是最简单的方法。
我要使用相同的代码和修改的时间,当我有6项显示。我认为,第二部分应该是“col s12 m6 l6”的2倍或4倍。
这里是全码为包括在哪里处理其他情况下(当我具有小于4级的职位以显示)的部分:
<?php
$my_query = new WP_Query($query_args);
$count = $my_query->post_count;
$i = 1;
query_posts($query_args);
while (have_posts()) :
the_post();
// For 1-4 create a single row
if($count == 1)$class = "col s12";
if($count == 2)$class = "col s12 m6 l6";
if($count == 3)$class = "col s12 m4 l4";
if($count == 4)$class = "col s12 m6 l3";
//For 5 videos
if($count == 5) {
$the_class = array(
'col s12 m6 l3',
'col s12 m6 l6'
);
$class = $the_class[array_rand($the_class)];
}
?>
<div class="row">
<div class="<?php echo $class; ?> featured-videos-item">
<?php if (has_post_thumbnail()): ?>
<div class="featured-videos-item-content featured-videos-low" style="background: url('<?php echo get_thumbnail_url($post->ID); ?>') 50% 50% no-repeat; background-size: cover;">
<?php endif; ?>
<div class="featured-videos-item-content--table">
<div class="featured-videos-item-content--cell">
<div class="overlay"></div>
<a class="featured-videos-link" href="<?php the_permalink(); ?>"></a>
<h3 class="featured-videos-title with-post--meta"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
</div>
</div>
</div>
</div>
<?php
$i++;
endwhile;
wp_reset_query();
?>
</div>
编辑:我已删除了嵌套如果。
编辑2:这里的链接的地方,我发现这个解决方案后Trying to get random array value to echo on element class
编辑3:$rand = array_rand($the_class);
似乎并不必要和代码工作没有它,但我仍然不知道如何追查或限制使用“col s12 m4 l6”的时间。
东西在你的。代码是错误的...非常错误,也许链接到你引用的前一个问题?顺便说一下,嵌套的if是不必要的;因为如果它在第一个if中,那么$ count已经是5 – Cunning
You将不得不跟踪列宽以前使用过多少次,如果超过某个阈值(2或4取决于大小),则不要再次选择该值(或将其从可用选项中删除)。 –
'$ rand = array_rand($ the_class)'的意义是什么;'当你不使用'$ rand'时?你在别的地方使用它吗? –