简体   繁体   English

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

[英]How to show related posts by category from different post type

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 . 其中之一是publication与自定义类别,类型, publication-category ,另一个是service与定制类型为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. 我目前正在使用ACF插件来执行此操作,但是每当有新的手册时,我都必须转到服务页面并在其中添加它。 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();
?>

If you're on services page, so why do you use the 'publication-category' in 如果您在服务页面上,那么为什么要使用“出版物类别”

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

Seems like you have to use 好像您必须使用

$custom_taxterms = get_the_terms($post->ID, 'service-category');

$terms = [];
foreach ($custom_taxterms as $term) {
  $terms[] = $term->slug
}

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

And create same slugs for both terms in both categories. 并为两个类别中的两个术语创建相同的子弹。 From my understanding. 根据我的理解。 It's quite difficult to understand your architecture 很难理解您的架构

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 我找到了解决方案,方法是将服务类别分类法更改为与其他帖子类型相同的发布类别,并使用以下代码从以下位置创建关系: https : //wordpress.stackexchange.com/questions/139571/display-posts-with-same -物种分类数据库长期?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();

                                      }?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM