代码之家  ›  专栏  ›  技术社区  ›  Tncmk

如何按类别显示不同职位类型的相关职位

  •  0
  • Tncmk  · 技术社区  · 8 年前

    在我的网站上,我有两种不同的帖子类型。其中之一是 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();
    ?>
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Vitali Protosovitski    8 年前

    如果您在“服务”页上,那么为什么在

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

    在两个类别中为两个术语创建相同的slug。据我所知。很难理解你的架构

        2
  •  0
  •   Tncmk    8 年前

    我通过将服务类别分类更改为发布类别(与其他post类型相同)并创建与以下代码的关系来找到解决方案: https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?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();
    
                                          }?>