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

WordPress按文章元数据控制Admin Bar显示问题求助

问题1:切换开关状态需再次点击才生效

问题根源

  1. 原代码注释掉了while循环,仅遍历每个文章类型的第一篇文章,导致状态显示不完整
  2. 表单提交后未即时刷新页面获取最新状态,且缺少安全验证
  3. 更新逻辑未明确是单篇文章还是批量更新文章类型

修复方案(单篇文章控制)

替换原页面代码为:

<?php
$post_types = get_post_types();
?>
<div class="container">
    <form id="tableform" method="post">
        <?php wp_nonce_field('update_admin_bar_status', 'admin_bar_nonce'); ?>
        <table class="table-style right">
            <thead>
                <th>文章类型</th>
                <th>状态</th>
                <th>文章标题</th>
            </thead>
            <?php
            foreach ($post_types as $post_type) {
                $args = array(
                    'post_type' => $post_type,
                    'post_status' => 'any',
                    'posts_per_page' => -1,
                );

                $the_query = new WP_Query($args);

                if ($the_query->have_posts()) {
                    while ($the_query->have_posts()) {
                        $the_query->the_post();
                        $post_id = get_the_ID();
                        $admin_bar_status = get_post_meta($post_id, 'admin_bar_status', true);
                        $admin_bar_status = $admin_bar_status !== '' ? $admin_bar_status : '1';
                        ?>
                        <tr>
                            <td><?php echo $post_type; ?></td>
                            <td align="center">
                                <label class="toggle-switchy" data-size="sm">
                                    <input type="checkbox" 
                                           id="toggle-<?php echo $post_id; ?>" 
                                           onchange="submitPostStatus(<?php echo $post_id; ?>, this.checked)"
                                           <?php checked($admin_bar_status, '1'); ?>>
                                    <span class="toggle">
                                        <span class="switch"></span>
                                    </span>
                                </label>
                            </td>
                            <td><?php echo get_the_title(); ?></td>
                        </tr>
                        <?php
                    }
                    wp_reset_postdata();
                }
            }
            ?>
        </table>
    </form>
    <script>
        function submitPostStatus(postId, isChecked) {
            const form = document.getElementById('tableform');
            const existingInputs = form.querySelectorAll('input[name="postId"], input[name="status"]');
            existingInputs.forEach(input => input.remove());
            
            const postIdInput = document.createElement('input');
            postIdInput.type = 'hidden';
            postIdInput.name = 'postId';
            postIdInput.value = postId;
            
            const statusInput = document.createElement('input');
            statusInput.type = 'hidden';
            statusInput.name = 'status';
            statusInput.value = isChecked ? '1' : '0';
            
            form.appendChild(postIdInput);
            form.appendChild(statusInput);
            form.submit();
        }
    </script>
</div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['admin_bar_nonce']) && wp_verify_nonce($_POST['admin_bar_nonce'], 'update_admin_bar_status')) {
    $post_id = intval($_POST['postId']);
    $status = $_POST['status'] === '1' ? '1' : '0';
    update_post_meta($post_id, 'admin_bar_status', $status);
    wp_redirect($_SERVER['REQUEST_URI']);
    exit;
}
?>

核心修改点

  • 恢复while循环,遍历每个文章类型下的所有文章
  • 添加WordPress安全nonce验证
  • 使用checked()函数简化选中状态判断
  • 提交后立即刷新页面,即时显示最新状态

问题2:无法隐藏指定页面的Admin Bar

问题根源

原代码遍历所有文章而非当前访问的文章,导致逻辑错误——只要有一篇文章状态为0,所有页面的Admin Bar都会被隐藏,而非仅目标页面。

修复方案(单篇页面控制)

替换原set_admin_bar函数为:

add_action('template_redirect', 'set_admin_bar');
function set_admin_bar() {
    if (!is_user_logged_in()) {
        return;
    }
    
    if (is_singular()) {
        $post_id = get_the_ID();
        $admin_bar_status = get_post_meta($post_id, 'admin_bar_status', true);
        
        if ($admin_bar_status === '0') {
            show_admin_bar(false);
        }
    }
}

按需扩展(批量隐藏文章类型所有页面)

如果需要该类型下只要有一篇设置为0,就隐藏所有该类型页面的Admin Bar,使用以下代码:

add_action('template_redirect', 'set_admin_bar');
function set_admin_bar() {
    if (!is_user_logged_in()) {
        return;
    }
    
    if (is_singular()) {
        $current_post_type = get_post_type();
        $has_hidden_post = get_posts(array(
            'post_type' => $current_post_type,
            'post_status' => 'any',
            'posts_per_page' => 1,
            'meta_query' => array(
                array(
                    'key' => 'admin_bar_status',
                    'value' => '0',
                    'compare' => '='
                )
            ),
            'fields' => 'ids'
        ));
        
        if (!empty($has_hidden_post)) {
            show_admin_bar(false);
        }
    }
}

核心修改点

  • 仅针对登录用户处理(非登录用户看不到Admin Bar)
  • 使用is_singular()判断当前是否为单篇内容页面
  • 仅读取当前访问文章的元数据,精准控制Admin Bar显示状态

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 12:23:24