在php中分配random类,但限制类的属性化时间
在一篇关于stackoverflow的帖子之后,我能够修改我的代码,将一个随机类分配给我的div。我现在面临的问题是,由于它是随机的,所以它部分地与ECS一起工作 基本上,当我有5或6篇文章要显示时,我想随机显示,并有不同的列大小。只有两个选项(要么是6列宽,要么是3列宽)。每个“行”总共应为12列 我的问题是:如何限制“col s12 m6l6”类的给出时间 代码如下:在php中分配random类,但限制类的属性化时间,php,class,random,Php,Class,Random,在一篇关于stackoverflow的帖子之后,我能够修改我的代码,将一个随机类分配给我的div。我现在面临的问题是,由于它是随机的,所以它部分地与ECS一起工作 基本上,当我有5或6篇文章要显示时,我想随机显示,并有不同的列大小。只有两个选项(要么是6列宽,要么是3列宽)。每个“行”总共应为12列 我的问题是:如何限制“col s12 m6l6”类的给出时间 代码如下: if($count == 5): $the_class = array( 'col s12 m6 l
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; ?>
当随机赋值是“col s12 m6 l6”的3倍时,它是完美的,但当它大于或小于“col s12 m6 l6”时,它不起作用。
我知道array_rand可能不是实现这一点的最佳方法,但它是迄今为止对我来说最简单的方法
当我有6个项目要显示时,我将使用相同的代码进行修改。我相信有2或4倍的“col s12 m6 l6”应该可以解决第二部分的问题
以下是完整的代码,包括我处理其他情况的部分(当我要显示的帖子少于4篇时):
请使用大括号;它使代码的可读性和可维护性提高了86倍
我认为这应该奏效:
如果($count>=5){
$l=数组(3,6);
$total_cols=0;
回声';
foreach($posts作为$post){
如果($total_cols<9){
$selected\u l=数组\u rand($l);
}
否则{
$l=3;
}
回声';
$total_cols+=$selected_l;
如果($total_cols==12){
$total_cols=0;
回声';
}
}
回声';
}
您可以使用此代码(如果我正确理解了您的问题)获得完整的12部分col空间。见:
您的代码中有错误。。。大错特错。是否可以链接到您提到的上一个问题?顺便说一下,嵌套if是不必要的;因为如果是在第一个if中,$count
已经是五次了。您必须跟踪列宽以前使用过多少次,如果它超过某个阈值(2或4,取决于大小),那么就不要再选择该值(或从可用选项中删除它)。$rand=array\u rand(\u类)的意义是什么代码>当您不使用$rand
时?你在其他地方使用它吗?你说12列,但你说3倍的“col s12 m6l6”这是完美的,但3 x 6=18???请解释你将5或6个帖子(两个?)分成多少行,并提供你将类归属于div的代码。4篇文章或7篇文章会发生什么情况?这个选项似乎不错,但我无法让它与我的代码一起工作,因为我没有使用foreach,它也不能与其余的代码一起工作(当我有4篇或更少的文章时,因为它们由$class显示),我已经在我的原始文章中添加了完整的代码。@SwayRaines我知道你已经接受了我的答案。你能让你的代码正常工作吗?我的回答只是作为一种逻辑调整,因为我意识到您的实际代码可能与我展示的不同。
<?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>
if($count >= 5){
$l = array(3, 6);
$total_cols = 0;
echo '<div class="row">';
foreach($posts as $post){
if($total_cols < 9){
$chosen_l = array_rand($l);
}
else{
$chosen_l = 3;
}
echo '<div class="col s12 m6 l'.$chosen_l.'"></div>';
$total_cols += $chosen_l;
if($total_cols === 12){
$total_cols = 0;
echo '</div><div class="row">';
}
}
echo '</div>';
}
<?php
!defined('MAX_LIFE')?define('MAX_LIFE',3):null;
$classes = array(
array('life' => MAX_LIFE%4 == 0 ? MAX_LIFE : (MAX_LIFE%4 == 1 ? MAX_LIFE+3 : (MAX_LIFE%4 == 2 ? MAX_LIFE+2 : MAX_LIFE+1)), 'class' => 'col s12 m6 l3', 'space' => 3),
array('life' => MAX_LIFE%2 ? MAX_LIFE+1 : MAX_LIFE, 'class' => 'col s12 m6 l6', 'space' => 6),
);
$memory = 0;
do
{
if ($classes[0]['life'] > 0 && $classes[1]['life'] > 0)
$idx = rand(0,1);
else if ($classes[0]['life'] > 0)
$idx = 0;
else if ($classes[1]['life'] > 0)
$idx = 1;
else
break;
if ($memory + $classes[$idx]['space'] > 12)
continue;
else if ($memory + $classes[$idx]['space'] == 12)
$memory = 0;
else
$memory += $classes[$idx]['space'];
$classes[$idx]['life']--;
echo $classes[$idx]['class'] . "\n";
} while(1);