问题
I'm using Wordpress 3.8 + Woocommerce 2.0 I need to change the class of the wrapper that Woocommerce generate when I use a shortcode.
I use this shortcode: [recent_products per_page="12"] And the output is:
<div class="woocommerce">
the_product_loop....
</div>
I want to obtain
<div class="MYCUSTOMCLASS">
the_product_loop....
</div>
But I can't find where I have to change the code... In class-wc-shortcodes.php file I've found the declaration of the function that generates the wrapper:
public static function shortcode_wrapper(
$function,
$atts = array(),
$wrapper = array(
'class' => 'woocommerce',
'before' => null,
'after' => null
)
)
But... I don't want to change the core files of Woocommerce plugin, It's possible to define my custom class via functions.php??
回答1:
You can create your own shortcode, just a clone of the default one, but with that change, so paste this in your functions.php:
function custom_recent_products_FX($atts) {
global $woocommerce_loop, $woocommerce;
extract(shortcode_atts(array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts));
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="MY_CUSTOM_CLASS">' . ob_get_clean() . '</div>';
}
add_shortcode('custom_recent_products','custom_recent_products_FX');
Notice at the end of that function the "MY_CUSTOM_CLASS", change that for your needs.
This will create a new shortcode, almost same than the "recent_products" one but with that only change.
So to output this, just use on template:
echo do_shortcode('[custom_recent_products per_page="3"]');
Or in your posts:
[custom_recent_products per_page="3"]
I don´t know if this is the best approach, but for what i can see, the class "woocommerce" is returned directly on the recent_products shortcode function as html, so i can´t imagine how to filter or hook this by anotherway.
Hope that helps and sorry my bad english :)
回答2:
you can use following trick to over right wrapper function :
Here i have created a shortcode for checkout and shortcode and change wrapper
shortcode will be [overright_checkout_of_shortcode]
function shortcode_handler($atts) {
$classget=new WC_Shortcodes();
$wrapper = array(
'class' => '',
'before' => '<div id="accordion" class="panel-group faq_group checkout-group" role="tablist" aria-multiselectable="true" >',
'after' => '</div>'
);
return $classget->shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts ,$wrapper);
}
add_shortcode('overright_checkout_of_shortcode','shortcode_handler');
来源:https://stackoverflow.com/questions/21482764/change-shortcode-wrapper-in-woocommerce