从多个类别中删除重复的帖子

时间:2018-12-19 13:48:39

标签: php wordpress

im试图显示选择的类别而没有任何重复,im只在wp查询循环中而不是在foreach中获取具有ID的数组,因此我无法将其传递给args(wp查询)以排除它们...这里的最佳解决方案是什么?

 <div class="row related-quest">
            <div class="col-12">
              <?php
              $terms = get_the_category();
              if( $terms ): ?>
              <?php foreach( $terms as $term ): ?>
              <?php
              // Get the ID of a given category
              $category_id = get_cat_ID( $term->name );
              // Get the URL of this category
              $category_link = get_category_link( $category_id );
              ?>
              <?php
                $do_not_duplicate = array();
              ?>
              @php $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => 3,
                   'post__not_in' => $do_not_duplicate,
                    'tax_query' => array(
                    array(
                    'taxonomy' => 'category',
                    'field' => 'term_id',
                    'terms' => $term
                    ))); @endphp
              @php $query = new WP_Query( $args ); @endphp
              @if ( $query->have_posts() )
                @while ( $query->have_posts() )
                  @php $query->the_post(); @endphp
                <?php
                  $post_id = get_the_ID();
                  $do_not_duplicate[] = $post_id;
                  ?>
                  <div <?php
                       $categories = get_the_category();
                       foreach ( $categories as $category ) {
                         echo '" alt="' . esc_attr( $category->name ) . '" ';
                       }
                       ?> class="questions">
                    <!-- Get the url/title of current post of this category -->
                    <a href="{{ the_permalink()  }}" class="content-link">{{ the_title()  }} </a>
                  </div>

                @endwhile
              @endif
              @php wp_reset_postdata(); @endphp
              <?php endforeach; ?>
              <?php endif; ?>
            </div>
          </div>

0 个答案:

没有答案