问题
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 categoryclass="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