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

如何通过页面ID移除特定WordPress页面的.fixed-top类?

移除特定页面头部的.fixed-top类方法

方法一:修改主题模板文件(header.php)

直接在输出头部类名的位置,用WordPress的条件标签is_page()判断目标页面,动态控制是否添加.fixed-top类。

假设你的头部HTML结构原本是:

<header class="site-header fixed-top">

改成以下代码,把需要移除类的页面ID(比如123、456)填入is_page()的参数中:

<?php
// 定义不需要fixed-top的页面ID数组
$exclude_pages = array(123, 456);
// 动态拼接类名
$header_classes = 'site-header';
if (!is_page($exclude_pages)) {
    $header_classes .= ' fixed-top';
}
?>
<header class="<?php echo esc_attr($header_classes); ?>">

这样指定页面就不会加载.fixed-top类,其他页面正常保留粘性效果。

方法二:用JavaScript动态移除类(无需修改模板)

如果不想改动模板文件,可以通过WordPress的钩子添加自定义JS,在特定页面加载时移除类。

在主题的functions.php里添加以下代码:

function remove_fixed_top_on_specific_pages() {
    // 指定要移除类的页面ID
    if (is_page(array(123, 456))) {
        ?>
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                const header = document.querySelector('.site-header');
                if (header) {
                    header.classList.remove('fixed-top');
                }
            });
        </script>
        <?php
    }
}
add_action('wp_footer', 'remove_fixed_top_on_specific_pages');

这段代码会在指定页面的页脚加载JS,页面加载完成后找到头部元素并移除.fixed-top类。

补充说明

  • 页面ID可以在WordPress后台「页面」编辑页的URL中查看(参数post=xxx里的xxx就是ID)。
  • 单个页面只需写成is_page(123),多个页面就把ID放进数组即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 09:47:03