【问题标题】:PHP Checking backwards in a loop, and writing conditions to avoid consecutive values with array_rand()PHP在循环中向后检查,并使用array_rand()编写条件以避免连续值
【发布时间】:2012-03-22 05:43:21
【问题描述】:

我的解决方案作为答案发布在下面

但是我不会接受我的答案是正确的。我非常感谢有关优化我发布的解决方案的任何帮助或建议。我会接受最有帮助的答案作为正确答案。感谢您的帮助!


在博客模板中的 wordpress 主题中,我正在使用三种尺寸的特色图片创建帖子预览,小中号和大号。

以下代码可以很好地随机显示不同尺寸的显示,我想避免三个或更多相同尺寸的连续帖子。

也就是说,如果连续有两个“中”,那么下一个应该是小或大,之后可以为后续数组项重新开始随机化。这意味着从 $thumbs 数组中删除“连续”值,然后将其重新添加到下一次迭代中。

if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); 

$thumbsize = get('thumb_size');

$thumbs = array();

$thumbs[0] = get_the_post_thumbnail($post->ID, 'locations-small');
$thumbs[1] = get_the_post_thumbnail($post->ID, 'locations-med');
$thumbs[2] = get_the_post_thumbnail($post->ID, 'locations');

$thumbid = get_post_thumbnail_id($post->ID);

if($thumbsize == 'Small'){
    $thumb = $thumb[0];
} elseif($thumbsize == 'Medium') {
    $thumb = $thumb[1];
} elseif($thumbsize == 'Large') {
    $thumb = $thumb[2];
} else {
    $thumb = $thumbs[array_rand($thumbs)];
}

// so on and so forth

我知道这需要在“if($wp_query)”循环中向后检查两个项目,但我不确定如何执行此操作...

如何使用 array_rand() 编写条件代码以避免连续值?

谢谢!

【问题讨论】:

    标签: php arrays wordpress random


    【解决方案1】:

    这是我的解决方案。我使用多个计数器来检查连续值,并在达到“连续限制”后重置计数器。

    如果有人对如何进一步优化此代码有任何建议,我当然会接受最有帮助的回答作为正确答案。谢谢!

    // three counters for medium, small and large
    $ism = 0;
    $im = 0;
    $il = 0;
    
    $args = array( 
        'post_type' => 'property',
        'paged' => $paged
    );
    
    query_posts( $args ); 
    
    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
        $thumbsize = get('thumb_size');
    
        $thumbs = array();
    
        $thumbsmall = get_the_post_thumbnail($post->ID, 'locations-small');
        $thumbmed = get_the_post_thumbnail($post->ID, 'locations-med');
        $thumblarge = get_the_post_thumbnail($post->ID, 'locations');
    
        // If no consecutive sizes have been counted, include all sizes in the array
        if($ism != 2 && $im != 2 && $il != 2) {
            $thumbs[0] = $thumbsmall;
            $thumbs[1] = $thumbmed;
            $thumbs[2] = $thumblarge;
        }
    
        $thumbid = get_post_thumbnail_id($post->ID);
    
        if($thumbsize == 'Small'){
            $thumb = $thumb[0];
        } elseif($thumbsize == 'Medium') {
            $thumb = $thumb[1];
        } elseif($thumbsize == 'Large') {
            $thumb = $thumb[2];
        } else {
            $thumb = $thumbs[array_rand($thumbs)];
        }
    
        // Check if each thumb size has been used and if that size occured in the previous iteration, if so, increment the counter, else reset the count to zero
        if($thumb == $thumbsmall && $ism <= 1) {$ism++;} else {$ism = 0;}
        if($thumb == $thumbmed && $im <= 1) {$im++;} else {$im = 0;}
        if($thumb == $thumblarge && $il <= 1) {$il++;} else {$il = 0;}
    
        // If there are two consecutive sizes, remove one item from the array and reset the counter to zero
        if($ism == 2) {
            $thumbs[0] = $thumblarge;
            $thumbs[1] = $thumbmed;
            $ism = 0;
        }
    
        if($im == 2) {
            $thumbs[0] = $thumbsmall;
            $thumbs[1] = $thumblarge;
            $im = 0;
        }
    
        if($il == 2) {
            $thumbs[0] = $thumbmed;
            $thumbs[1] = $thumbsmall;
            $il = 0;
        }
    
        // So on and so forth
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2019-11-06
      相关资源
      最近更新 更多