如何分配此代码以仅在特定类别上显示?

时间:2019-07-09 13:56:08

标签: wordpress woocommerce

在我们的产品页面上,我们有墙纸类别。对于该类别中的每个产品,我们要显示此代码。

我们可以使它显示在所有产品上,并且编写此代码是为了排除类别,但我们只想显示设置的类别。

我们正在使用Permalink Wordprss插件从网址中删除/ product /或/ product-category/。

add_action( 'woocommerce_after_add_to_cart_form', 'content_after_addtocart_button' );
function content_after_addtocart_button() {
echo '<div style="clear: both; margin-bottom: 30px"></div>
<div class="product-buttons">
<a href="/wallpaper-samples/">Order Sample</a></div>';
}
add_filter( 'woocommerce_shortcode_products_query' , 'exclude_cat_shortcodes');

function exclude_cat_shortcodes($query_args){

    $query_args['tax_query'] =  array(array( 
            'taxonomy' => 'product_cat', 
            'field' => 'slug', 
            'terms' => array('wallpaper'), // Don't display products from this category
            'operator' => 'AND'
        )); 

    return $query_args;
}

这应该可以,但是不能。

'add_action( 'woocommerce_after_add_to_cart_form', 'content_after_addtocart_button' );
function content_after_addtocart_button() {
echo '<div style="clear: both; margin-bottom: 30px"></div>
<div class="product-buttons">
<a href="/wallpaper-samples/">Order Sample</a></div>';
}
add_filter( 'woocommerce_shortcode_products_query' , 'exclude_cat_shortcodes');

function exclude_cat_shortcodes($query_args){

    $query_args['tax_query'] =  array(array( 
            'taxonomy' => 'product_cat', 
            'field' => 'slug', 
            'terms' => array('wallpaper'), // Don't display products from this category
            'operator' => 'AND'
        )); 

    return $query_args;
}'

要在屏幕上显示“订购样品”按钮,仅适用于“墙纸”产品类别。

1 个答案:

答案 0 :(得分:0)

添加它。 这将工作100%。 添加此参数-> 'terms' => $categories_id

array(
      'taxonomy' => 'our_project_category',
      'field'    => 'term_id',
      'terms'    => $categories_id,
    )