我试图从一个类别中列出带有自定义分类法和几个子自定义分类的帖子。
子自定义分类法“知识库”
子自定义分类法“优化”
but结果是重复发布列表,而不是按自定义分类法:
子自定义分类法“知识库”
子自定义分类法“优化”
例如:代码:
$taxonomies = get_terms(
array(
'taxonomy' => 'tutorial',
'hide_empty' => false,
)
);
// Loop through categories
foreach ( $taxonomies as $taxonomy ) {
// Display Taxonomy name
echo '' . $taxonomy->name . '';
echo '';
// WP_Query arguments
$args = array(
'cat' => 2, // wordpress category
'terms' => name,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>发布于 2019-11-07 09:31:20
您的帖子列表是重复的,因为在您的WP_Query args中,您只从ID为"2“的类别中获取帖子。
为了从自定义分类法中获得帖子,您需要在您的tax_query中使用WP_Query。您可以在这里阅读有关tax_query的更多信息:https://developer.wordpress.org/reference/classes/wp_查询/#分类法-参数
我要做以下几点:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 2,//Your Wordpress category here
),
array(
'taxonomy' => 'tutorial',
'field' => 'term_id',
'terms' => $taxonomy->term_id,
),
),
);
$query = new WP_Query( $args );https://wordpress.stackexchange.com/questions/352081
复制相似问题