在数组中使用对象的属性

问题描述 投票:1回答:1

我想使用“高级自定义字段”来确定将显示哪些类别的产品。 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
      )
    )
);
php arrays wordpress object advanced-custom-fields
1个回答
1
投票

查看文档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'
      )
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.