duanmeng3126 2018-08-10 03:38
浏览 29
已采纳

如何按类别显示不同帖子类型的相关帖子

In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.

I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.

Perhaps you guys can help me how I can arrange the code a way that works for my above request.

<?php

$custom_taxterms = wp_get_object_terms( 
    $post->ID, 
    'publication-category', 
    array( 'fields' => 'ids' ) 
);

$args = array(
    'post_type'      => 'publication',
    'post_status'    => 'publish',
    'posts_per_page' => 10,
    'orderby'        => 'rand',
    'order'          => 'ASC',
    'tax_query'      => array( array(
        'taxonomy' => 'publication-category',
        'field'    => 'id',
        'terms'    => $custom_taxterms
    )),
    'post__not_in'   => array( $post->ID ),
);

$related_items = new WP_Query( $args );

if ( $related_items->have_posts() ) :
    echo '<ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;
    echo '</ul>';
endif;

wp_reset_postdata();
?>
  • 写回答

2条回答 默认 最新

  • douqi0090 2018-08-13 05:10
    关注

    I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1

    Thanks everyone

      <?php
                                              //get the post's venues
                                          $custom_terms = wp_get_post_terms($post->ID, 'publication-category');
    
                                          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' => 'publication-category',
                                                      'field' => 'slug',
                                                      'terms' => $custom_term->slug,
                                                  );
    
                                              }
    
                                              // put all the WP_Query args together
                                              $args = array( 'post_type' => 'publication',
                                                              'posts_per_page' => 20,
                                                              'tax_query' => $tax_query );
    
                                              // finally run the query
                                              $loop = new WP_Query($args);
    
                                              if( $loop->have_posts() ) {
    
                                                  while( $loop->have_posts() ) : $loop->the_post(); ?>
    
                                                  <div class="listing-title"><?php the_title(); ?></div>
                                                <div class="listing-image"><a href="<?php the_permalink() ?>" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID(), 'full') ?>')"></a>
                                                </div>
                                                  <?php
    
                                                  endwhile;
    
                                              }
    
                                              wp_reset_query();
    
                                          }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b