Wordpress从搜索结果中列出自定义分类术语

时间:2018-01-17 15:41:48

标签: php wordpress advanced-custom-fields

我创建了一个名为' projects'的自定义帖子类型。使用自定义分类法。我创建了多个项目,并使用名为' company'的自定义分类标记这些项目。

我想要做的是当我搜索在多家公司中标记的项目时。是有一个名为'公司'在仅列出公司的搜索结果页面上。

我现在所拥有的是,当我搜索并显示多个项目时,它复制了公司'分类学'但我只想展示一个分类并删除任何重复。

我现在的代码是:

<?php                   
  // the query
  $search_query = new WP_Query( $args ); ?>

  <?php if ( $search_query->have_posts() ) : ?>

    <ul class="search-result">

      <!-- pagination here -->

        <!-- the loop -->
        <?php while ( $search_query->have_posts() ) : $search_query->the_post(); ?>
        <li>
          <?php echo get_the_term_list( $post->postname, 'companies', 'Disipline: ', ', ' ); ?>
        </li>
        <?php endwhile; ?>
        <!-- end of the loop -->

        <!-- pagination here -->

    </ul>

    <?php wp_reset_postdata(); ?>

    <?php else : ?>
      <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

如果我有2个项目的搜索结果,我会得到2个很多的术语列表,即电影,视频,电影......但我只是想说它电影,视频

1 个答案:

答案 0 :(得分:0)

我设法解决了我的问题所以发布了我的答案。

<div class="search-container">
  <h2 class="pagetitle">In: <span>Discipline</span></h2>
    <ul class="search-result">
      <?php
        $_terms = get_terms( array('sectors') );
        foreach ($_terms as $term) :
        $term_slug = $term->slug;
        $term_link = get_term_link( $term );
        $_posts = new WP_Query( array(
          'post_type'         => 'projects',
          's'                 => $s,
          'posts_per_page'    => 10, //important for a PHP memory limit warning
          'tax_query' => array(
            array(
              'taxonomy' => 'sectors',
              'field'    => 'slug',
              'terms'    => $term_slug,
            ),
        ),
    ));
    if( $_posts->have_posts() ) :
      echo '<li>'. '<h3>' . $term->name . '</h3>';
      echo '<p>' . $term->description . '</p>';
      // We successfully got a link. Print it out.
      echo '<a href="' . esc_url( $term_link ) . '">' . 'view page' .         '</a></li>';
    ?>

    <?php
      endif;
        wp_reset_postdata();
        endforeach;
    ?>
   </ul>
</div>
相关问题