wordpress中的相关帖子类别(分类)

时间:2017-02-09 04:28:47

标签: php wordpress

如何处理此代码只显示“按类别划分的相关帖子”? 这段代码在“single.php”里面 我没有stackoverflow但无法找到解决方案。任何人都可以帮助我吗?

browser.runtime.getURL('/options.html')

3 个答案:

答案 0 :(得分:1)

首先,您需要使用此功能获取当前帖子术语

get_the_terms ( get_the_id(), 'your_custom_taxonomy' );

然后你在WP_Query中获取tax_query的条件slug。

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

我希望你解决问题。

答案 1 :(得分:0)

在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');

你可以试试这个..

答案 2 :(得分:0)

如果您想通过简单的帖子类别检索帖子,请检查以下代码:

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

希望,这会对你有帮助。

相关问题