繁体   English   中英

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

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

在我的网站上,我有两种不同的帖子类型。 其中之一是publication与自定义类别,类型, publication-category ,另一个是service与定制类型为service-category 我在发布页面上发布了一些手册,这些手册使用的服务不同。 我想要做的是按相同类别类型在“服务”页面中显示这些小册子。 就像,如果该小册子是由教育服务部门发布的,则此小册子应显示在教育服务页面上。

我目前正在使用ACF插件来执行此操作,但是每当有新的手册时,我都必须转到服务页面并在其中添加它。 今天,我尝试使用以下代码,但它显示了来自不同类别类型而不是相同类别类型的所有手册。

也许你们可以为我提供帮助,以便我按照上述要求安排代码。

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

如果您在服务页面上,那么为什么要使用“出版物类别”

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

好像您必须使用

$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
    )),
);

并为两个类别中的两个术语创建相同的子弹。 根据我的理解。 很难理解您的架构

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

感谢大家

  <?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