Sakurairo 主题 SEO 优化:修复报“多 H1 标签”与页面索引问题

FengJiayou 发布于 7 小时前 27 次阅读


1.前言

在用 WordPress + Sakurairo 主题配合 Rank Math SEO 插件提交 Bing 站长工具(Bing Webmaster Tools)时,可能会遇到页面被提示 “发现了但没有爬行 / URL无法出现在必应上”,经 URL Inspection 检测还会跳出 More than one h1 tag(存在多个 H1 标签) 的 SEO 警告。

根据 Google & Bing 的 HTML 语义化规范,一个标准网页有且仅能包含一个 <h1> 标签(即主标题),而 Sakurairo 默认的首页与文章页模板在多处硬编码了 <h1>,导致搜索引擎判定页面结构不规范。

2.修改方案

主要影响代码在外观 (Appearance) -> 主题文件编辑器 (Theme File Editor) -> 首页模板(index.php)

把代码替换为已下代码即可= ̄ω ̄=

本文使用的主题版本:3.0.10。不同版本可能有出入
<?php
get_header();

// 获取组件顺序数据
$component_order = iro_opt('homepage_components', []) ? iro_opt('homepage_components', []) : array();

// 按顺序动态渲染组件
foreach ($component_order as $component) {
    switch ($component) {
        // 静态页面
        case 'static_page':
            $static_page_id = iro_opt("static_page_id");
            if ($static_page_id && ($static_page = get_post($static_page_id))) :
                ?>
                <section class="custom-static-section">
                    <h2 class="main-title static-page-title">
                        <?php if (!(strpos(get_the_title($static_page), "_") === 0)) {
                            # '_'开头的静态页面不显示标题
                            echo esc_html(get_the_title($static_page));
                        }
                        ?>
                    </h2>
                    <div class="static-page-content">
                        <?php echo apply_filters('the_content', $static_page->post_content); ?>
                    </div>
                </section>
            <?php
            endif;
            break;
            
        // 特色区域
        case 'exhibition':
                get_template_part('exhibition');
            break;

        // 文章列表
        case 'primary':
            ?>
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
                    <h2 class="main-title posts-area-title">
                        <i class="<?php echo esc_attr(iro_opt('post_area_icon', 'fa-regular fa-bookmark')); ?>" aria-hidden="true"></i>
                        <?php echo esc_html(iro_opt('post_area_title', '文章列表')); ?>
                    </h2>

                    <?php if (have_posts()) : ?>
                        <?php if (is_home() && !is_front_page()) : ?>
                            <header class="archive-header">
                                <h2 class="page-title screen-reader-text"><?php single_post_title(); ?></h2>
                            </header>
                        <?php endif; ?>

                        <?php get_template_part('tpl/content', 'thumb'); ?>

                    <?php else : ?>
                        <?php get_template_part('tpl/content', 'none'); ?>
                    <?php endif; ?>
                </main>

                <?php if (iro_opt('pagenav_style') == 'ajax') : ?>
                    <div id="pagination"><?php next_posts_link(__(' Previous', 'sakurairo')); ?></div>
                    <div id="add_post">
                        <span id="add_post_time" style="visibility: hidden;" 
                            title="<?php echo esc_attr(iro_opt('page_auto_load', '')); ?>">
                        </span>
                    </div>
                <else : ?>
                    <nav class="traditional-pagination">
                        <echo paginate_links(array(
                            'base'      => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
                            'format'    => '?paged=%#%',
                            'current'   => max(1, get_query_var('paged')),
                            'total'     => $wp_query->max_num_pages,
                            'prev_text' => '<i class="fa-solid fa-angle-left"></i>',
                            'next_text' => '<i class="fa-solid fa-angle-right"></i>'
                        )); ?>
                    </nav>
                <endif; ?>
            </div>
            <?php
            break;
    }
}

if (!in_array('primary', $component_order)) : //是否需要提供虚假的首页标记,解决当文章不显示时封面丢失,兼容前端js
    ?>
        <main id="main" class="site-main">
            <h2 class="main-title posts-area-title" style="display:none;">
            </h2>
        </main>
<?php
endif;

get_footer();
?>

3.详细改动

  1. 静态页面标题(原版第 16 行):把 <h1 class="main-title static-page-title"> 修改为 <h2>
  2. 文章区域标题(原版第 36 行): 这是最主要的原因(即你在主题设置里看到的 Article 或 文章列表 标题)。 把 <h1 class="main-title posts-area-title"> 修改为 <h2>
  3. 隐藏的屏幕阅读器 / 兼容标题(原版第 44 行与第 78 行)

最后贴上我的源文件,方便各位参考

<?php
get_header();

// 获取组件顺序数据
$component_order = iro_opt('homepage_components', []) ? iro_opt('homepage_components', []) : array();

// 按顺序动态渲染组件
foreach ($component_order as $component) {
    switch ($component) {
        // 静态页面
        case 'static_page':
            $static_page_id = iro_opt("static_page_id");
            if ($static_page_id && ($static_page = get_post($static_page_id))) :
                ?>
                <section class="custom-static-section">
                    <h2 class="main-title static-page-title">
                        <?php if (!(strpos(get_the_title($static_page), "_") === 0)) {
                            # '_'开头的静态页面不显示标题
                            echo esc_html(get_the_title($static_page));
                        }
                        ?>
                    </h2>
                    <div class="static-page-content">
                        <echo apply_filters('the_content', $static_page->post_content); ?>
                    </div>
                </section>
            <?php
            endif;
            break;
            
        // 特色区域
        case 'exhibition':
                get_template_part('exhibition');
            break;

        // 文章列表
        case 'primary':
            ?>
            <div id="primary" class="content-area">
                <main id="main" class="site-main" role="main">
                    <h2 class="main-title posts-area-title">
                        <i class="<echo esc_attr(iro_opt('post_area_icon', 'fa-regular fa-bookmark')); ?>" aria-hidden="true"></i>
                        <echo esc_html(iro_opt('post_area_title', '文章列表')); ?>
                    </h2>

                    <?php if (have_posts()) : ?>
                        <?php if (is_home() && !is_front_page()) : ?>
                            <header class="archive-header">
                                <h2 class="page-title screen-reader-text"><single_post_title(); ?></h2>
                            </header>
                        <?php endif; ?>

                        <get_template_part('tpl/content', 'thumb'); ?>

                    <else : ?>
                        <get_template_part('tpl/content', 'none'); ?>
                    <endif; ?>
                </main>

                <if (iro_opt('pagenav_style') == 'ajax') : ?>
                    <div id="pagination"><next_posts_link(__(' Previous', 'sakurairo')); ?></div>
                    <div id="add_post">
                        <span id="add_post_time" style="visibility: hidden;" 
                            title="<echo esc_attr(iro_opt('page_auto_load', '')); ?>">
                        </span>
                    </div>
                <else : ?>
                    <nav class="traditional-pagination">
                        <echo paginate_links(array(
                            'base'      => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
                            'format'    => '?paged=%#%',
                            'current'   => max(1, get_query_var('paged')),
                            'total'     => $wp_query->max_num_pages,
                            'prev_text' => '<i class="fa-solid fa-angle-left"></i>',
                            'next_text' => '<i class="fa-solid fa-angle-right"></i>'
                        )); ?>
                    </nav>
                <endif; ?>
            </div>
            <?php
            break;
    }
}

if (!in_array('primary', $component_order)) : //是否需要提供虚假的首页标记,解决当文章不显示时封面丢失,兼容前端js
    ?>
        <main id="main" class="site-main">
            <h2 class="main-title posts-area-title" style="display:none;">
            </h2>
        </main>
<?php
endif;

get_footer();
?>