首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将类传递给Woocommerce短代码[relative_products]

将类传递给Woocommerce短代码[relative_products]
EN

WordPress Development用户
提问于 2020-04-23 20:09:51
回答 1查看 1.3K关注 0票数 0

这些天我一直在使用Woocommerce的代码(Woocommerce 3.8)。

对于许多用户来说,您可以将"class“作为一个值传递--例如,产品短代码:[products class="custom-class"],然后返回内容的包装器将具有custom-class

But不适合!??

如果您有动态设计,并且希望能够根据页面上显示的内容更改相关产品的设计,则非常有用。

如果您查看伍尔商业博士的底部,您可以看到以下函数:

代码语言:javascript
复制
    public static function related_products( $atts ) {
        if ( isset( $atts['per_page'] ) ) {
            $atts['limit'] = $atts['per_page'];
        }

        // @codingStandardsIgnoreStart
        $atts = shortcode_atts( array(
            'limit'    => '4',
            'columns'  => '4',
            'orderby'  => 'rand',
        ), $atts, 'related_products' );
        // @codingStandardsIgnoreEnd

        ob_start();

        // Rename arg.
        $atts['posts_per_page'] = absint( $atts['limit'] );

        woocommerce_related_products( $atts );

        return ob_get_clean();
    }

正如您所看到的,shortcode_atts中没有类?

How来改变这个?

EN

回答 1

WordPress Development用户

发布于 2020-04-23 20:09:51

I找到了一个解决方案,但我不确定它有多好

1)将这些短代码复制到functions.php

如果您从上面的链接到Woocommerce文档复制函数并使用它创建自己的短代码(it_related_products_function),然后将'class' => ''添加到参数中,那么您现在可以将'class‘传递给您的短代码。

代码语言:javascript
复制
function it_related_products_function( $atts ) {
 if ( isset( $atts['per_page'] ) ) {
   $atts['limit'] = $atts['per_page'];
 }

 $atts = shortcode_atts( array(
   'limit'    => '4',
   'columns'  => '4',
   'orderby'  => 'rand',
   'class'    => '',
 ), $atts, 'related_products' );

 ob_start();

 $atts['posts_per_page'] = absint( $atts['limit'] );

 woocommerce_related_products( $atts );

 return ob_get_clean();

}

(

2)获取related.php中的属性并回显(如果设置为

)

代码语言:javascript
复制
    ">3)现在您可以用“class”anywhere使用自定义的短代码
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/364891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档