我只是仔细检查了一下,WooCommerce通过the_content
过滤器,这意味着它应该运行短代码。
WooCommerce有很多短代码,see their documentation
包括特色产品:
[featured_products per_page="12" columns="4"]
这样做的缺点是,特色产品可能并非都来自该特定类别。你没有提到这是否是一个问题。
如果是,则可以从[featured_products]
(这实际上只是运行一个二次循环WP_Query
), 稍微调整一下,将输出添加到woocommerce_before_shop_loop
钩
function wpa_107952_featured (){
if( !is_product_category() )
return;
$args = array(
\'post_type\' => \'product\',
\'product_cat\' => get_query_var(\'product_cat\'),
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => 1,
\'posts_per_page\' => 8,
\'meta_query\' => array(
array(
\'key\' => \'_visibility\',
\'value\' => array(\'catalog\', \'visible\'),
\'compare\' => \'IN\'
),
array(
\'key\' => \'_featured\',
\'value\' => \'yes\'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop[\'columns\'] = 4;
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();
echo \'<div class="woocommerce">\' . ob_get_clean() . \'</div>\';
}
add_action( \'woocommerce_before_shop_loop\', \'wpa_107952_featured\' );