WordPress中ACF字段无法与FooGallery配合使用的问题求助
Hey there, let's work through this frustrating FooGallery and ACF issue you're hitting. I’ve dealt with this exact scenario before, so here’s what’s going wrong and how to fix it:
The Core Problem
When you use get_field() without passing a post ID, it defaults to using the current post/page ID (the one your sidebar is loaded on). But your ACF fields are attached to the foogallery custom post type entries—not the parent post. That’s why get_field() is returning empty, and do_shortcode() only renders the gallery itself, not your custom fields.
Solution 1: Extract Gallery ID from Post Content (For Shortcode-Inserted Galleries)
If you’re adding FooGallery to posts via the [foogallery id="XXX"] shortcode, you can pull that ID directly from the post content and use it to fetch your ACF fields. Add this to your static sidebar PHP file:
// Grab the current post's content $post_content = get_the_content(); // Use regex to pull the FooGallery ID from the shortcode preg_match('/\[foogallery\s+id="(\d+)"\]/', $post_content, $matches); if (!empty($matches[1])) { $gallery_id = $matches[1]; // Fetch your ACF fields using the gallery's ID $custom_gallery_title = get_field('your_acf_title_field_name', $gallery_id); $custom_gallery_desc = get_field('your_acf_description_field_name', $gallery_id); // Output the fields safely in your sidebar if ($custom_gallery_title) { echo '<h4 class="gallery-sidebar-title">' . esc_html($custom_gallery_title) . '</h4>'; } if ($custom_gallery_desc) { echo '<p class="gallery-sidebar-desc">' . esc_html($custom_gallery_desc) . '</p>'; } } else { // Fallback if no gallery is found in the post echo '<p>No gallery details available</p>'; }
Solution 2: Use an ACF Relationship Field (Cleaner Approach)
If you’re not tied to shortcodes, set up an ACF Relationship Field on your post type that lets you select a FooGallery entry. This makes fetching the gallery ID way more reliable:
- Create an ACF Relationship field (e.g., named
linked_gallery) on your post type, set the "Post Type" filter tofoogallery. - In your sidebar code, use this to fetch the gallery and its fields:
// Get the linked gallery from the post's relationship field $linked_gallery = get_field('linked_gallery'); if ($linked_gallery) { // Relationship fields return an array—grab the first gallery (adjust if using multiple) $gallery_id = $linked_gallery[0]->ID; // Fetch your custom fields $custom_gallery_title = get_field('your_acf_title_field_name', $gallery_id); $custom_gallery_desc = get_field('your_acf_description_field_name', $gallery_id); // Output as before if ($custom_gallery_title) { echo '<h4 class="gallery-sidebar-title">' . esc_html($custom_gallery_title) . '</h4>'; } if ($custom_gallery_desc) { echo '<p class="gallery-sidebar-desc">' . esc_html($custom_gallery_desc) . '</p>'; } }
Quick Debug Tips
- Double-check your ACF field names (they’re case-sensitive!) and confirm they’re assigned to the
foogallerypost type in ACF settings. - Add
var_dump($gallery_id);right after you fetch it to make sure you’re getting a valid number (not empty or null). - Test
var_dump(get_field('your_field_name', $gallery_id));to see if ACF is returning any data at all—this can help spot field assignment issues.
内容的提问来源于stack exchange,提问作者Eric

