更改 WooCommerce 短代码输出中的标记 [英] Change markup in WooCommerce shortcode output

查看:57
本文介绍了更改 WooCommerce 短代码输出中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 WooCommerce 短代码,例如[product_category category="appliances"] 修改标记以在某些元素周围包含

标签?

我知道我可以通过复制短代码的作用来做到这一点,但仅仅添加一个包装

似乎有点矫枉过正.

我可以用 jQuery 来完成,但不希望它们的加载有任何延迟.

解决方案

@更新

是的,这绝对有可能.您可以通过以下方式实现:

if( !function_exists('prod_cat') ) {函数 prod_cat( $atts ) {提取(shortcode_atts(数组('猫' =>'', //类别'类' =>''//可选地将类添加到 <div>标签), $atts));如果( $class != ''){$class = 'class="' . $class .'"';}返回 '​​

'.do_shortcode("[product_category category=" . $cat . "]") .'</div>';}add_shortcode('prod_cat', 'prod_cat');}

将上述代码片段粘贴到活动子主题或主题的function.php文件中

此代码经过测试且功能齐全.

<小时>

用法:

shortcode 有 2 个参数:

  • cat="your_category" 设置您的类别
  • class="your_div_class" 将 css 类设置为

    标签 (可选).

您可以像任何简码一样在任何 woocommerce 页面中使用它:

[prod_cat cat="appliances" class="my_optional_div_class"]

Is it possible to use a WooCommerce shortcode e.g. [product_category category="appliances"] amending the markup to include a <div> tag around some elements?

I know I can do this by duplicating what the shortcode does but that seems overkill just to add a wrapping <div>.

I could do it with jQuery but don't want any delay in them loading.

解决方案

@Updated

Yes this is absolutely possible. You can achieve this with something like that:

if( !function_exists('prod_cat') ) {

    function prod_cat( $atts ) {

        extract(shortcode_atts(array(
            'cat'   => '',  // category
            'class' => '' // Optional adding class to <div> tag


        ), $atts));

        if ( $class != '') {
            $class = ' class="' . $class . '"';
        }

        return '<div'.$class.'>' . do_shortcode("[product_category category=" . $cat . "]") . '</div>';

    }

    add_shortcode('prod_cat', 'prod_cat');
}

Paste the above code snippet in the function.php file of active child theme or theme

This code is tested and fully functional.


USAGE:

You have 2 arguments for this the shortcode:

  • cat="your_category" to set your category
  • class="your_div_class" to set css classes to the <div> tag (optional).

You can use it in any woocommerce page as any shortcode does:

[prod_cat cat="appliances" class="my_optional_div_class"]

这篇关于更改 WooCommerce 短代码输出中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆