在数组中使用对象的 属性
Using an object's property in array
我想使用高级自定义字段来确定将显示哪个类别的产品。 ACF 字段给出了类别的 ID,我可以从中获取 slug。我无法让它在数组中工作,所以它在页面上显示了所有产品。有没有人有建议或想法为什么它不起作用?提前致谢!
$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,您似乎缺少 tax_query
数组中的 field
属性。
所以你的 $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'
)
)
);
我想使用高级自定义字段来确定将显示哪个类别的产品。 ACF 字段给出了类别的 ID,我可以从中获取 slug。我无法让它在数组中工作,所以它在页面上显示了所有产品。有没有人有建议或想法为什么它不起作用?提前致谢!
$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,您似乎缺少 tax_query
数组中的 field
属性。
所以你的 $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'
)
)
);