You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress主题中Redux与ACF作者显示多条件判断实现咨询

How to Control Post Author Display with Redux Framework + 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_author is set to yes: 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 if true, hide if false)

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_name matches the opt_name you 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 default case 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:26:04