drfu80954 2018-12-19 13:48
浏览 43

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

im trying to show picked categories without any duplicates, im getting array with ids only in wp query loop and not in foreach so i cannot pass it down for args ( wp query ) to exclude them... what's best solution here ?

 <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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向