如何创建WordPress短代码按Post ID显示指定自定义Testimonial
Solution for Testimonial Shortcode with ID Filtering
Got it, let's build that custom shortcode for your testimonials CPT. This implementation will support both the posts_per_page and testimonial_id parameters you specified, and it's flexible enough to fit your theme's styling needs.
Here's the complete code you can add to your theme's functions.php file (or a custom plugin if you prefer):
function testimonial_shortcode($atts) { // Extract shortcode attributes with sensible defaults $atts = shortcode_atts(array( 'posts_per_page' => 2, // Show 2 testimonials by default 'testimonial_id' => '' // No specific IDs by default ), $atts, 'testimonial'); // Prepare our custom query arguments $args = array( 'post_type' => 'testimonials', 'posts_per_page' => intval($atts['posts_per_page']), 'post_status' => 'publish', 'orderby' => 'post__in' // Preserve the order of the IDs you input ); // If specific testimonial IDs are provided, parse them into an integer array if (!empty($atts['testimonial_id'])) { $testimonial_ids = array_map('intval', explode(',', $atts['testimonial_id'])); $args['post__in'] = $testimonial_ids; } // Run the query to fetch our testimonials $testimonials_query = new WP_Query($args); // Start output buffering to capture our HTML ob_start(); // Check if we have testimonials to display if ($testimonials_query->have_posts()) { echo '<div class="testimonials-shortcode-wrapper">'; while ($testimonials_query->have_posts()) { $testimonials_query->the_post(); ?> <div class="testimonial-item"> <?php // Show featured image if available ?> <?php if (has_post_thumbnail()) : ?> <div class="testimonial-thumbnail"> <?php the_post_thumbnail('thumbnail'); ?> </div> <?php endif; ?> <h3 class="testimonial-title"><?php the_title(); ?></h3> <div class="testimonial-content"><?php the_content(); ?></div> <?php // Show author if set ?> <?php if (get_the_author()) : ?> <p class="testimonial-author">— <?php echo esc_html(get_the_author()); ?></p> <?php endif; ?> </div> <?php } echo '</div>'; // Reset post data to avoid conflicts with the main query wp_reset_postdata(); } else { echo '<p>No testimonials found.</p>'; } // Return the captured HTML content return ob_get_clean(); } add_shortcode('testimonial', 'testimonial_shortcode');
How It Works
- Shortcode Registration: We use
add_shortcode()to tell WordPress to recognize the[testimonial]shortcode and link it to our function. - Attribute Handling:
shortcode_atts()sets default values (2 testimonials by default) and safely merges them with any parameters you pass in. - Query Filtering: When you provide
testimonial_id, we split the comma-separated string into an array of integers and usepost__into fetch only those specific posts. We also setorderbytopost__inso the testimonials appear in the same order as the IDs you list. - Output Structure: The loop generates clean, classed HTML for each testimonial. You can tweak the classes (like
testimonials-shortcode-wrapperortestimonial-item) to match your theme's CSS and style them however you want. - Cleanup:
wp_reset_postdata()ensures we don't interfere with WordPress's main query after our custom loop runs.
Usage Examples
- Show 2 specific testimonials:
[testimonial testimonial_id="123,145"] - Show 5 testimonials (no ID filter):
[testimonial posts_per_page="5"] - Show 3 specific testimonials in your desired order:
[testimonial posts_per_page="3" testimonial_id="789,456,123"]
内容的提问来源于stack exchange,提问作者Poonam Katpara
相关产品推荐
相关产品推荐

