我想使用“高级自定义字段”来确定将显示哪些类别的产品。 ACF字段提供了类别的ID,从中我可以得到该子弹。但是我无法使其在数组中工作,因此它在页面上显示了所有产品。有人对它不起作用有任何建议或想法吗?预先感谢!
$term_id = get_field('kies_product_categorie'); //Get category id
$term = get_term_by('id', $term_id, 'product_cat'); //Get terms from given category
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug //Slug of given category
)
)
);
查看文档here,您似乎缺少field
数组内的tax_query
属性。
所以您的$ args应该看起来像:
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug, //Slug of given category
'field' => 'slug'
)
)
);