簡體   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