我不理解我的问题的行为
我尝试显示带有税的帖子。好的,很常见
这是我的代码:
$myTools = get_the_term_list( $post->ID,\'tool\');
echo $myTools;//just to test
$args_technique = array(
\'post_type\' => \'technic\', // my custom post
\'posts_per_page\' => 4, // How many items to display
\'no_found_rows\' => true,// no pagination to speed up the query
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'tool\',
\'field\' => \'name\',
\'terms\' => $myTools
)
),
);
//My loop
$technique_query = new wp_query( $args_technique );
if ( $technique_query->have_posts() ) { ?>
while ( $technique_query->have_posts() ) {
$technique_query->the_post(); ?>
the_post_thumbnail();
the_title();
}// End while
wp_reset_postdata();
}//end if
它起作用了<但前提是我的分类法“工具”中只有一个术语。只要我添加另一个术语,它就不会显示任何内容。。。我的回声测试有效(=它能很好地显示不同的术语),但不能显示。。。
我试了一下foreach
而不是while
.
相同的未命中。。。我看了一下我的BDD,但这似乎很正常。这可能是因为我的注册税不分等级吗?有人能告诉我我的错误吗<非常奇怪
SO网友:Cha
好的,找到了,抱歉吵闹
//fetch the terms of my tax
$terms = wp_get_post_terms( $post->ID, \'tool\');
$terms_ids = []; //an array, which can contain several terms
foreach ( $terms as $term ) {
$terms_ids[] = $term->term_id;
}
// Query
$args = array(
\'post_type\' => \'technical_post\',
\'posts_per_page\' => 4, // How many items to display
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'tool\',
\'field\' => \'term_id\',
\'terms\' => array($term->term_id) )
),
);
// Query posts
$tech_query = new wp_query( $args );
// Loop through posts
foreach( $tech_query->posts as $post ) : setup_postdata( $post );
the_title();
// End loop
endforeach;
// Reset post data
wp_reset_postdata();