首页 文章

2自定义帖子类型一个共享分类

提问于
浏览
0

我有两个自定义Post类型(系列和音频)和一个共享自定义分类 . One Series帖子将有多个相关的音频帖子 . 我试图通过一个类别将它们联系起来 . 我想在系列页面上显示所有相关的音频帖子 . 我试过循环分类,但我得到一系列的帖子或所有的系列和音频帖子 . 任何想法如何做到这一点?

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();
    
      }?>
    

相关问题