Change markup in WooCommerce shortcode output

跟風遠走 提交于 2019-12-31 03:09:08

问题


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.


回答1:


@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"]


来源:https://stackoverflow.com/questions/38803702/change-markup-in-woocommerce-shortcode-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!