您可以创建自己的简码,只是默认简码的克隆,但要进行更改,因此请将其粘贴到您的 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');
请注意该函数末尾的“MY_CUSTOM_CLASS”,根据您的需要进行更改。
这将创建一个新的短代码,与“recent_products”几乎相同,但只有变化。
所以要输出这个,只需在模板上使用:
echo do_shortcode('[custom_recent_products per_page="3"]');
或在您的帖子中:
[custom_recent_products per_page="3"]
我不知道这是否是最好的方法,但就我所见,“woocommerce”类直接在recent_products 短代码函数上作为html 返回,所以我无法想象如何过滤或挂钩换个方式。
希望对我有帮助,对不起我的英语不好:)