【问题标题】:Detect elements with same background-image value and remove检测具有相同背景图像值的元素并删除
【发布时间】:2017-03-07 20:08:55
【问题描述】:

我正在显示帖子的提要,其中一些帖子使用相同的背景图片。我想找到并隐藏重复的。

完整代码:

 <?php 
    $args=array(
                'cat' => '1',
                'post_status' => 'publish',
                'post_type' => 'post',
                'posts_per_page' => 10,
                'taxonomy' => 'postkicker',
                'term' => 'vote',
                'orderby'    => 'date',
                'order'      => 'DESC'
    );

    $my_query = new WP_Query($args);

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

   $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),  array( 284,110 ), false, ''  );

 ?>

    <div class="divider-left">
     <a class="img-thumbnew" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
     <div style="background-image: url(<?php echo $src[0]; ?> );"></div>
     </a>
     </div>

【问题讨论】:

    标签: php jquery duplicates background-image


    【解决方案1】:

    我没有看到循环发生在哪里。但是这里一个穷人的答案是跟踪循环中看到的 src,然后在找到一个时中止

    <?php
       $srcs_seen = array();
       while ( // looping over stuff ) {
    
           $src = wp_get_attachment_image_src(
               get_post_thumbnail_id($post->ID),  array( 284,110 ), false, ''  
           );
    
           // keep looping but stop here if we've seen it
           if (in_array($src['url'], $srcs_seen)) continue; 
           $srcs_seen[] = $src['url'];
    
           // print html like you were doing
       }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-12-22
      • 1970-01-01
      • 2020-08-17
      • 2011-05-22
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2016-06-22
      • 2021-01-04
      相关资源
      最近更新 更多