【问题标题】:Prevent duplicate posts within foreach防止 foreach 中的重复帖子
【发布时间】:2018-05-20 23:07:02
【问题描述】:

大家好,我目前正在处理一个 foreach 循环,该循环从 Wordpress 后端的图库中提取图像。画廊工作正常,正如预期的那样,但是,我想防止出现重复的照片。

<?php if () $query->have_posts() ):
  $thumbs = [];
?>
<?php 
    foreach( $query->posts as $gallery ): 
      $images = get_field('gallery_images', $gallery->ID);
      if( $images ):
        foreach( $images as $image ):
          $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
          $img_id[] = $image['ID'];
          $thumbs[] = $cropped_img;             
?>

与问题无关的 HTML

<?php endforeach; endif; endforeach; ?>
<?php foreach($thumbs as $thumbnail): ?>
  <div class="gallery__thumb"><img src="<?= $thumbnail; ?>"></div>
<?php endforeach; ?>

这基本上就是我想要解决的问题。我想为图像 ID 添加两个变量,这样它们就不会在第一个代码块中重复。所以第一个 if/foreach 状态看起来像:

<?php if( $images ):
  foreach( $images as $image ):
    $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
    $img_id[] = $image['ID'];
    $do_not_duplicate[] = $image['ID'];
    $thumbs[] = $cropped_img;
?>

其次是缩略图的following foreach

<?php foreach($thumbs as $thumbnail): ?>
    <?php foreach($img_id as $img): ?>
      <?php if ($img == $do_not_duplicate) : ?>
      //do nothing
      <?php else : ?>
      <div class="gallery__thumb"><img src="<?= $thumbnail; ?>"></div>
      <?php endif; ?>
<?php endforeach; endforeach; ?>

我遇到的问题是我得到了 22 张图像输出 23 次,因为有 22 张图像被拉出(有 1 张重复)。本质上,我正在尝试完成 22 张没有重复的图像。

任何见解将不胜感激,在此先感谢您。

【问题讨论】:

    标签: php wordpress if-statement foreach


    【解决方案1】:

    像这样简单地编辑你的 php 代码:

    $thumbs[$image['ID']] = $cropped_img;
    

    这将是一个关联数组,其中图像以 id 作为键存储,因此如果将翻倍的图像推送到数组中,它将覆盖旧的图像

    【讨论】:

      【解决方案2】:

      根据您的代码,应该是:

      <?php if( $images ):
        foreach( $images as $image ):
          $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
          $img_id[] = $image['ID'];
          $thumbs['url'][] = $cropped_img;
          $thumbs['id'][] = $image['ID']
      ?>
      

      然后

      <?php foreach($thumbs as $thumbnail): ?>
          <?php if (!in_array($thumbnail['do_not_duplicate'], $img_id)): ?>
            <div class="gallery__thumb"><img src="<?= $thumbnail['url']; ?>"></div>
          <?php endif; ?>
      <?php endforeach; ?>
      

      【讨论】:

        【解决方案3】:

        您可以在添加之前检查图像 id 是否已存在于 $img_id 数组中。

        我在下面修改了你的代码

        <?php if () $query->have_posts() ):
          $thumbs = [];
        ?>
        <?php 
            foreach( $query->posts as $gallery ): 
              $images = get_field('gallery_images', $gallery->ID);
              if( $images ):
        
                // Initialize $img_id[]
                $img_id = [];
        
                foreach( $images as $image ):
        
                  // Only add to the $img_id array and $thumbs array if
                  //   $image['ID'] is not in $img_id array
                  if( ! in_array($image['ID'], $img_id)):
                    $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
                    $img_id[] = $image['ID'];
                    $thumbs[] = $cropped_img;             
        ?>
        

        希望这会有所帮助!

        根据您的 $image['ID'] 值是什么,您可能需要传递使用 in_array() 的第三个参数来进行严格检查。

        参考:http://php.net/manual/en/function.in-array.php

        【讨论】:

          猜你喜欢
          • 2020-07-27
          • 1970-01-01
          • 2021-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-30
          • 1970-01-01
          相关资源
          最近更新 更多