Php 如何在wordPress中通过分类类别id或分类类别名称获取分类类别帖子的所有帖子?
我创建了post类型的产品&还创建了名为productcategory的分类法。 现在我需要获得相关产品的特定类别。 不知何故,我通过使用Php 如何在wordPress中通过分类类别id或分类类别名称获取分类类别帖子的所有帖子?,php,wordpress,Php,Wordpress,我创建了post类型的产品&还创建了名为productcategory的分类法。 现在我需要获得相关产品的特定类别。 不知何故,我通过使用 $term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids")); 现在我怎样才能得到这个分类类别的所有帖子 $terms = get_the_terms( $post->ID, 'productcategory' );
$term_list = wp_get_post_terms($post->ID, 'productcategory', array("fields" => "ids"));
现在我怎样才能得到这个分类类别的所有帖子
$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 );
然后,只需使用此查询的结果运行一个循环,就可以开始了 显示与特定分类相关的帖子。如果指定注册到自定义post类型的分类法,则不使用“category”,而是使用“{custom_taxonomy_name}”。例如,如果您有一个名为“流派”的自定义分类法,并且只想显示来自“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 );
?>
使用
WP\u查询
并专门查看参数