WordPress自定义短码:Swiper滑块无法获取相册图片等问题求助
Hey there, let's tackle your three WordPress shortcode issues one by one—these are common pitfalls I’ve worked through before, so here’s what you need to do:
1. Fix get_post_gallery_images() returning an empty array
The core problem here is that get_post_gallery_images() only looks for top-level [gallery] shortcodes directly in your post content. Since your [gallery] is nested inside [bs-slider], the function can’t find it. Plus, there are two other easy-to-miss issues in your code:
- Shortcode attributes are always passed as strings, so checking
=== truewill never match yourpagination="true"input. - Swiper requires every slide to be wrapped in a
<div class="swiper-slide">element—your original code was missing this, so even if images loaded, the slider wouldn’t render correctly.
Here’s the fixed imgSlider function that parses nested galleries and fixes these issues:
public function imgSlider( $attr, $content ) { global $post; $a = shortcode_atts(array( 'pagination' => 'false', 'buttons' => 'false', 'scrollbar' => 'false' ), $attr); $images = array(); // Extract nested [gallery] shortcode attributes if (preg_match('/\[gallery\s(.*?)\]/', $content, $matches)) { $gallery_attr = shortcode_parse_atts($matches[1]); // Grab image IDs from the gallery's 'ids' parameter if (!empty($gallery_attr['ids'])) { $image_ids = explode(',', $gallery_attr['ids']); foreach ($image_ids as $id) { // Use the size specified in the gallery shortcode (default to 'large') $size = !empty($gallery_attr['size']) ? $gallery_attr['size'] : 'large'; $image_url = wp_get_attachment_image_url(trim($id), $size); if ($image_url) { $images[] = $image_url; } } } } $slider = ''; if( !empty($images) ){ $slider = '<div class="swiper-container">'; $slider .= '<div class="swiper-wrapper">'; foreach( $images as $img ){ // Add required swiper-slide wrapper $slider .= '<div class="swiper-slide"><img class="img-fluid w-100" src="'. esc_url($img) .'"></div>'; } $slider .= '</div>'; // Check against string values since shortcode attrs are strings if( $a['pagination'] === 'true' ){ $slider .= '<div class="swiper-pagination"></div>'; } if( $a['buttons'] === 'true' ){ $slider .= '<div class="swiper-button-prev"></div>'; $slider .= '<div class="swiper-button-next"></div>'; } $slider .= '</div>'; } return $slider; }
2. Remove default WordPress gallery shortcode
To ensure WordPress doesn’t parse the default [gallery] shortcode (and interfere with your custom slider), add this to your plugin’s initialization (e.g., your main class constructor):
public function __construct() { // Unregister the default gallery shortcode remove_shortcode('gallery'); // Register your custom slider shortcode add_shortcode('bs-slider', array($this, 'imgSlider')); }
If you want your [bs-slider] to handle all instances of the default [gallery] shortcode too, add this line alongside the others:
add_shortcode('gallery', array($this, 'imgSlider'));
3. Remove auto-added <p> and <br> tags
WordPress’s wpautop filter automatically inserts these tags around content, including shortcodes. Here are two reliable fixes:
Option 1: Fix globally for all shortcodes
Add these filters to your plugin to reorder how wpautop and shortcodes are processed:
// Disable default wpautop order remove_filter('the_content', 'wpautop'); // Run wpautop before shortcodes are parsed add_filter('the_content', 'wpautop', 99); // Remove p/br tags that wrap shortcodes add_filter('the_content', 'shortcode_unautop', 100);
Option 2: Target only your bs-slider shortcode
If you don’t want to affect other content, clean up the output specifically for your slider:
add_filter('do_shortcode_tag', function($output, $tag) { if ($tag === 'bs-slider') { // Remove empty p tags wrapping slider elements $output = preg_replace('#<p>\s*(</?div>)\s*</p>#', '$1', $output); // Remove stray br tags $output = preg_replace('#<br\s*/?>#', '', $output); } return $output; }, 10, 2);
内容的提问来源于stack exchange,提问作者ussarin0

