0

我有两种自定义帖子类型(系列和音频),其中一种共享自定义分类法。一个系列帖子将有多个相关的音频帖子。我正在尝试按类别将它们联系起来。我想在系列页面上显示所有相关的音频帖子。我曾尝试遍历分类法,但我要么得到一个系列文章列表,要么得到所有系列文章和音频文章。任何想法如何做到这一点?

4

1 回答 1

0

这行得通。

//get the post's Tax
  $custom_terms = wp_get_post_terms($post->ID, 'series_taxonomy');

  if( $custom_terms ){

      // going to hold our tax_query params
      $tax_query = array();

      // add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
      if( count( $custom_terms > 1 ) )
          $tax_query['relation'] = 'OR' ;

      // loop through venus and build a tax query
      foreach( $custom_terms as $custom_term ) {

          $tax_query[] = array(
              'taxonomy' => 'series_taxonomy',
              'field' => 'slug',
              'terms' => $custom_term->slug,
          );

      }

      // put all the WP_Query args together
      $args = array( 'post_type' => 'sermon',
                      'posts_per_page' => 5,
                      'tax_query' => $tax_query );

      // finally run the query
      $loop = new WP_Query($args);

      if( $loop->have_posts() ) {

          while( $loop->have_posts() ) : $loop->the_post(); ?>

          <a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></div></a>
          <?php

          endwhile;

      }

      wp_reset_query();

  }?>
于 2018-07-24T16:52:30.610 回答