Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce短代码显示特定产品类别的销售产品_Php_Wordpress_Woocommerce_Shortcode_Product - Fatal编程技术网

Php Woocommerce短代码显示特定产品类别的销售产品

Php Woocommerce短代码显示特定产品类别的销售产品,php,wordpress,woocommerce,shortcode,product,Php,Wordpress,Woocommerce,Shortcode,Product,我有不同的类别,但我想在我的网站菜单中有一个项目,我将显示一个特定类别的销售产品,而不是所有的销售产品 我使用此代码,但它显示所有在售产品 [sale_products per_page="32"] 有什么提示吗?这是一个基于woocommerce 3.2版的自定义快捷码,现有快捷码将动态获取当前产品类别(在单个产品页面中),并显示“正在销售”的产品。您也可以定义特定的产品类别 您还可以(在函数代码内部或在短代码本身中)设置以下参数: 类别==>特定产品类别 限制==>要显示的默认产品数量

我有不同的类别,但我想在我的网站菜单中有一个项目,我将显示一个特定类别的销售产品,而不是所有的销售产品

我使用此代码,但它显示所有在售产品

[sale_products per_page="32"]

有什么提示吗?

这是一个基于woocommerce 3.2版的自定义快捷码,现有快捷码将动态获取当前产品类别(在单个产品页面中),并显示“正在销售”的产品。您也可以定义特定的产品类别

您还可以(在函数代码内部或在短代码本身中)设置以下参数:

  • 类别
    ==>特定产品类别
  • 限制
    ==>要显示的默认产品数量
  • columns
    =>默认列数
守则:

// Only for Woocommerce version 3.2 and higher (3.2+)
add_shortcode("cat_sale_prod", "get_category_on_sale_products");
function get_category_on_sale_products( $atts ) {
    global $post;

    // Attributes
    $atts = shortcode_atts(
        array(
            'limit'    => '12',
            'columns'  => '4',
            'category'  => '',
        ),
        $atts, 'cat_sale_prod');

    $limit   = $atts['limit'];
    $columns = $atts['columns'];

    // Get product categories
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'slugs' ));
    $term  = reset($terms);

    return do_shortcode( "[products limit='$limit' columns='$columns' on_sale='true' category='$term']" );
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作

使用特定产品类别的示例:

[cat_sale_prod category="clothing"]

Woocommerce附带了一些短代码,可用于向页面添加自定义内容

[products]快捷码允许您按post ID、SKU、类别、属性显示产品,并支持分页、随机排序和产品标签,取代了对多个快捷码的需求,例如:

[featured_products]
[sale_products]
[best_selling_products] 
[recent_products] 
[product_attribute]
[top_rated_products]
因此,您可以将[products]快捷码与某些属性结合使用,并在您的类别中显示正在销售的产品,您只需使用类似的代码即可

[products  category ="slug-of-your-cat" on_sale="true" ]
有关更多参考资料,请访问:

不,它不起作用,它仍然显示所有销售情况,我的woocommerce是最新版本。@NajdDergham很抱歉,但对我来说,它在woocommerce版本3.2.x和3.3.x上起作用。虽然这可能是正确的答案,但请编辑并添加一些解释/详细信息,以便其他人能够理解它是如何解决问题的。