简体   繁体   English

如何在wordPress中按分类法类别ID或分类法类别名称获取所有分类法类别的帖子?

[英]How can i get all post of taxonomy category post by taxonomy category id or taxonomy category name in wordPress?

I have created post type of products & also created taxonomy called productcategory. 我创建了post类型的产品,还创建了名为productcategory的分类法。 Now i need to get particular category in related products. 现在我需要在相关产品中获得特定类别。 Somehow i managed to find the taxonomy category id & name by using 不知何故,我设法通过使用找到分类法类别ID和名称

$term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids"));

Now how can i get all post of this taxonomy category ? 现在,我如何获得该分类学类别的所有帖子?

Show posts associated with certain taxonomy. 显示与某些分类相关的帖子。 If specifying a taxonomy registered to a custom post type then instead of using 'category' you would use '{custom_taxonomy_name}'. 如果指定注册到自定义帖子类型的分类法,则不使用“类别”,而是使用“{custom_taxonomy_name}”。 For instance, if you had a custom taxonomy called "genre" and wanted to only show posts from the "jazz" genre you would use the below code. 例如,如果你有一个名为“genre”的自定义分类,并且只想显示“jazz”类型的帖子,你可以使用下面的代码。

 <?php
$args = array(
     'posts_per_page' => 8,
     'orderby' => 'rand',
     'post_type' => 'products',
     'productcategory' => 'your_cat_name',
     'post_status' => 'publish'
);
$show_albums = get_posts( $args );
?>
$terms = get_the_terms( $post->ID, 'productcategory' );    

$args = array(
'post_type' => 'products',
'tax_query' => array(
    array(
        'taxonomy' => 'productcategory',
        'field'    => 'slug',
        'terms' => explode(',',$terms),
    ),
),
);
$query = new WP_Query( $args );

Then just run a loop using the results of this query and you should be good to go! 然后使用此查询的结果运行循环,你应该好好去!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM