簡體   English   中英

Wordpress(分類法)中的相關文章類別

[英]Related posts category in wordpress (taxonomy)

如何使用此代碼僅顯示“按類別分類的相關帖子”? 這段代碼在“ single.php”里面,我沒有stackoverflow但找不到解決方案。 誰能幫我?

<?php  
     $postsPerPageq = 5;

               $allargw = array(
              'post_type' => 'audioplayer',
              'posts_per_page' => $postsPerPageq,
               'orderby' => 'title',
              'order' => 'DESC',
             'tax_query' => array(
        array(
            'taxonomy' => 'audiotop',
            'field' => 'slug',
            'terms' => 'top-audio'
        )
     )
             );
               $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();  


    ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>


<?php endwhile;   wp_reset_postdata();?>

首先,您需要使用此功能來獲取當前的職位條款

get_the_terms ( get_the_id(), 'your_custom_taxonomy' );

那么您就可以在WP_Query中獲取tax_query的條款。

$postsPerPageq = 5;
$terms_slugs = array();

//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' ); 
foreach ( $terms as $term) {
   $terms_slugs[] = $term->slug;
}

//WP_Query args
$allargw = array(
  'post_type' => 'audioplayer',
  'posts_per_page' => $postsPerPageq,
  'orderby'   => 'title',
  'order'     => 'DESC',
  'tax_query' => array(
     array(
       'taxonomy' => 'audiotop',
       'field' => 'slug',
       'terms' => $terms_slugs,
     )
   )
);

我希望它能解決您的問題。

在您的allargw數組值中添加類別名稱。

<?php  
  $postsPerPageq = 5;

           $allargw = array(
          'post_type' => 'audioplayer',
          'posts_per_page' => $postsPerPageq,
           'category_name'=>'Your category name',
           'orderby' => 'title',
          'order' => 'DESC',
         'tax_query' => array(
    array(
        'taxonomy' => 'audiotop',
        'field' => 'slug',
        'terms' => 'top-audio'
    )
 )
         );
           $topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();  


?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>

希望這對您有幫助!

更新的答案:

如果要自動添加類別,請像這樣使用。

wp_set_object_terms( 'your_post_id', 'your_category_name', 'your_taxonomy_name');

你可以試試看

如果要按簡單帖子類別檢索帖子,請檢查以下代碼:

<?php  
     $category = get_the_category($post->ID);
     $cat_id = $category[0]->term_id;
     $postsPerPageq = 5;
     $allargw = array(
              'post_type' => 'audioplayer',
              'posts_per_page' => $postsPerPageq,
               'orderby' => 'title',
              'order' => 'DESC',
              'cat' => $cat_id

            );
    $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();   ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
    <?php endwhile;   wp_reset_postdata();?>

否則,如果要按自定義分類檢索帖子,請檢查以下代碼:

<?php  
     $term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all"));
     $term_id = $term_list[0]->term_id ;
     $postsPerPageq = 5;
     $allargw = array(
                'post_type' => 'audioplayer',
                'posts_per_page' => $postsPerPageq,
                'orderby' => 'title',
                'order' => 'DESC',
                'tax_query' => array(
            array(
                'taxonomy' => 'audiotop',
                'field' => 'id',
                'terms' => $term_id
            )
        )
    );
    $topaudio = new WP_Query($allargw);
    while ( $topaudio->have_posts() ) : $topaudio->the_post();  ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
    <?php endwhile;   wp_reset_postdata();?>

希望這個能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM