如何限制主页上WooCommerce最近产品滑块的产品类别

时间:2014-01-24 16:04:06

标签: php wordpress woocommerce

我使用Athena(1.0.17)主题创建了一个新的WooCommerce(2.0.20)WordPress(3.8.1)网站。主页有一个最近的产品滑块。我有一个名为“文章”的产品类别,我不想在最近的产品滑块中出现。我认为我需要改变的代码是

$number_of_products = $settings['shop_area_entries'];
$args = array(
    'post_type' => 'product',
    'posts_per_page' => intval( $number_of_products ),
    'meta_query' => array( array(
        'key' => '_visibility',
        'value' => array('catalog', 'visible'),
        'compare' => 'IN'
        ))
);

$first_or_last = 'first';
$loop = new WP_Query( $args );
$query_count = $loop->post_count;
$count = 0;

有人能告诉我如何更改$ args以便只在WP_Query中返回非“类别”类别的产品吗?

1 个答案:

答案 0 :(得分:2)

WP_Query Woocommerce products that belong in distinct multiple categories only tax_query的帮助下(不知道为什么我在第一次通过之前的问题时没有找到这个!)我发现我需要做的就是将我的args改为:

$args = array(
'post_type' => 'product',
'product_cat' => 'books',
'posts_per_page' => intval( $number_of_products ),
'meta_query' => array( array(
    'key' => '_visibility',
    'value' => array('catalog', 'visible'),
    'compare' => 'IN'
    ))
);

如果我想添加更多类别,我只需要添加逗号分隔的目录slug列表。