如何通过短码或循环在首页显示WooCommerce特色产品及描述?
Great question! The default [featured_products] shortcode doesn’t include product descriptions out of the box, but you have two solid options to add them—let’s break down both so you can pick what works best for you:
Option 1: Create a Custom Shortcode (Reusable & Shortcode-Friendly)
If you want to stick with the shortcode pattern (so you can reuse this block elsewhere easily), you can build a custom shortcode that fetches featured products and includes the description. Here’s how:
Add this code to your child theme’s functions.php file (or a custom plugin):
function custom_featured_products_with_description() { ob_start(); // Query featured products $args = array( 'post_type' => 'product', 'posts_per_page' => 8, // Adjust number of products to show 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ), ), ); $products = new WP_Query( $args ); if ( $products->have_posts() ) { echo '<ul class="products">'; while ( $products->have_posts() ) { $products->the_post(); global $product; ?> <li class="product"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a> <h2 class="woocommerce-loop-product__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php echo $product->get_price_html(); ?> <?php woocommerce_template_loop_add_to_cart(); ?> <!-- Add product description --> <div class="product-description"> <?php echo wp_trim_words( $product->get_description(), 20 ); // Trim to 20 words (adjust as needed) ?> </div> </li> <?php } echo '</ul>'; wp_reset_postdata(); } return ob_get_clean(); } add_shortcode( 'featured_products_with_desc', 'custom_featured_products_with_description' );
Then replace your existing shortcode with [featured_products_with_desc] in your template. This keeps the shortcode workflow but adds the description, and you can reuse it anywhere on your site.
Option 2: Use a Custom Loop (Full Control Over Markup)
If you don’t need reusability and want direct control over every part of the HTML (like adjusting the layout without relying on shortcode defaults), replacing the shortcode with a custom loop is the way to go. Here’s how to update your existing code:
<section id="solutions-wrap"> <div class="container"> <?php // Query featured products $args = array( 'post_type' => 'product', 'posts_per_page' => 8, // Adjust number of products 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ), ), ); $products = new WP_Query( $args ); if ( $products->have_posts() ) { echo '<ul class="products">'; while ( $products->have_posts() ) { $products->the_post(); global $product; ?> <li class="product"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a> <h2 class="woocommerce-loop-product__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php echo $product->get_price_html(); ?> <?php woocommerce_template_loop_add_to_cart(); ?> <div class="product-description"> <?php echo wp_trim_words( $product->get_description(), 20 ); ?> </div> </li> <?php } echo '</ul>'; wp_reset_postdata(); } ?> </div> </section>
This gives you full control over how each product is displayed—you can tweak the HTML structure, add classes, or adjust the description length easily.
Which Should You Choose?
- Go with the custom shortcode if you want to use this featured products block on multiple pages/posts (it’s reusable and keeps your template clean).
- Go with the custom loop if this is only for the homepage and you want complete control over the markup without relying on shortcode logic.
Don’t forget to add some CSS for the .product-description class to style it properly (like adjusting margins, font size, etc.)!
内容的提问来源于stack exchange,提问作者Jake Lambourn

