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

在add_settings_section的before_section数组中添加条件判断出错求助

问题分析与解决

你的问题出在PHP三元运算符的优先级上,字符串拼接符(.)的优先级比三元运算符(?:)高,导致条件判断逻辑被错误拆解,无法正确执行,所以无论当前选中的tab是什么,最终都会输出display:none;。

错误代码的逻辑问题

原代码中:

'before_section' => '<div class="tab-content" id="tab_main" style="'.$_GET['tab']=="tab_main"?"":"display:none;" .'">',

实际执行顺序是先把<div...style="和$_GET['tab']拼接,再和"tab_main"做相等判断,最后才执行三元运算,完全偏离了你的预期逻辑。

修正方案

给三元表达式加上括号,让它优先执行:

add_settings_section(
    'team_section_main',
    '',
    '',
    'team_searchbox',
    array(
        'before_section' => '<div class="tab-content" id="tab_main" style="'.($_GET['tab'] == "tab_main" ? "" : "display:none;").'">',
        'after_section' => '</div>'
    )
);
add_settings_section(
    'team_section_destination',
    '',
    '',
    'team_searchbox',
    array(
        'before_section' => '<div class="tab-content" id="tab_destination" style="'.($_GET['tab'] == "tab_destination" ? "" : "display:none;").'">',
        'after_section' => '</div>'
    )
);

额外建议:安全处理输入

直接使用$_GET['tab']存在XSS风险,建议先对其进行过滤:

$current_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'tab_main';

add_settings_section(
    'team_section_main',
    '',
    '',
    'team_searchbox',
    array(
        'before_section' => '<div class="tab-content" id="tab_main" style="'.($current_tab == "tab_main" ? "" : "display:none;").'">',
        'after_section' => '</div>'
    )
);
// 同理处理第二个section

内容的提问来源于stack exchange,提问作者udarts

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 17:14:55