WordPress主题中Redux与ACF作者显示多条件判断实现咨询
Got it, let's work through this logic step by step. The core idea here is that post-specific ACF settings should override the global Redux options—that's the standard behavior users expect for content-level overrides.
The Rule Set First
Let's formalize the logic to make it crystal clear:
- If the ACF field
opt_field_authoris set toyes: Always show the author, regardless of the global setting - If the ACF field is set to
no: Always hide the author, no matter what the global panel says - If the ACF field is set to
inherit(or not set at all): Fall back to the global Redux setting$opt_panel_author(show iftrue, hide iffalse)
The Implementation Code
Here's a clean, readable way to implement this in your theme (place this in your single post template, or a template part that handles post meta):
// Fetch our settings // ACF post-level setting (pass post ID if using outside the loop: get_field('opt_field_author', $post_id)) $opt_field_author = get_field('opt_field_author'); // Redux global setting (replace 'your_redux_opt_name' with your actual Redux option name) $opt_panel_author = get_option('your_redux_opt_name')['panel_author']; // Initialize our display flag $display_author = false; // Apply our logic rules switch ($opt_field_author) { case 'yes': $display_author = true; break; case 'no': $display_author = false; break; case 'inherit': default: // Fall back to the global setting if we inherit or the field isn't set $display_author = $opt_panel_author; break; } // Render the author if allowed if ($display_author) { echo '<div class="post-author-meta"> <span class="author-label">By:</span> <a href="' . get_author_posts_url(get_the_author_meta('ID')) . '">' . get_the_author() . '</a> </div>'; }
Key Notes to Avoid Issues
- Redux Option Name: Double-check that
your_redux_opt_namematches theopt_nameyou set when initializing Redux Framework—this is the top-level key where your global options are stored in the WordPress options table. - ACF Field Context: If you're using this code outside the main post loop (like in a sidebar widget or custom query), make sure to pass the post's ID to
get_field()(e.g.,get_field('opt_field_author', 123)where 123 is the post ID). - Default Behavior: The
defaultcase handles scenarios where the ACF field might not be set (e.g., old posts created before the field was added)—it defaults to inheriting the global setting, which is a safe, user-friendly fallback.
内容的提问来源于stack exchange,提问作者John Quimera
相关产品推荐
相关产品推荐

