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

WordPress子主题index.php缺失<h1>标签及代码失效问题解决

WordPress子主题index.php修复:添加归档页H1标签与变量输出问题解决

一、为作者/分类等归档页面添加H1标签

当前index.php作为默认模板渲染作者页、分类页、标签页等归档页面时缺失H1标签,需在文章循环开始前添加页面类型判断,输出对应H1标题:

在<div id="left-area">之后、if ( have_posts() ) :之前插入以下代码:

<?php
// 根据页面类型输出对应H1标题
if ( is_author() ) {
    $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
    echo '<h1 class="archive-title">作者:' . esc_html( $author->display_name ) . '</h1>';
} elseif ( is_category() ) {
    echo '<h1 class="archive-title">分类:' . single_cat_title( '', false ) . '</h1>';
} elseif ( is_tag() ) {
    echo '<h1 class="archive-title">标签:' . single_tag_title( '', false ) . '</h1>';
} elseif ( is_home() && ! is_front_page() ) {
    echo '<h1 class="archive-title">' . single_post_title( '', false ) . '</h1>';
}
?>

二、修复echo $thumb . $alttext无法正常显示的问题

问题分析

  • $thumb是get_thumbnail()返回的完整<img>标签HTML,其中已经包含$alttext作为alt属性,直接拼接输出$alttext会导致纯文本显示在图片外部,不符合需求。
  • 后续代码中通过print_thumbnail()重复输出缩略图,导致之前的echo $thumb被覆盖或位置混乱。

解决方案

场景1:调试变量值

如果只是要验证变量内容,替换原echo $thumb . $alttext;为调试代码:

// 输出变量原始内容(前端查看源代码可见)
var_dump($thumb, $alttext);
// 或输出为页面可见的格式化文本
echo '<div style="margin:10px 0; padding:10px; background:#f5f5f5;">调试:thumb=' . htmlspecialchars($thumb) . '<br>alttext=' . esc_html($alttext) . '</div>';

场景2:直接使用$thumb显示缩略图

如果要替换原有缩略图输出逻辑,删除后续print_thumbnail()相关代码,直接使用:

echo $thumb;

修改后的完整index.php代码

<?php get_header(); ?>

<div id="main-content">
    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">
                <?php
                // 添加归档页H1标签
                if ( is_author() ) {
                    $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
                    echo '<h1 class="archive-title">作者:' . esc_html( $author->display_name ) . '</h1>';
                } elseif ( is_category() ) {
                    echo '<h1 class="archive-title">分类:' . single_cat_title( '', false ) . '</h1>';
                } elseif ( is_tag() ) {
                    echo '<h1 class="archive-title">标签:' . single_tag_title( '', false ) . '</h1>';
                } elseif ( is_home() && ! is_front_page() ) {
                    echo '<h1 class="archive-title">' . single_post_title( '', false ) . '</h1>';
                }
                ?>
                <?php
                    if ( have_posts() ) :
                        while ( have_posts() ) : the_post();
                            $post_format = et_pb_post_format(); echo $post_format; ?>

                            <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>

                        <?php
                            $thumb = '';

                            $width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );

                            $height    = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
                            $classtext = 'et_pb_post_main_image';
                            $titletext = get_the_title();
                            $alttext   = get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );
                            $thumbnail = get_thumbnail( $width, $height, $classtext, $alttext, $titletext, false, 'Blogimage' );
                            $thumb     = $thumbnail["thumb"];

                            // 替换为调试代码或直接输出thumb
                            var_dump($thumb, $alttext);
                            // echo $thumb; // 如果直接显示缩略图,用这行替代上面的var_dump

                            et_divi_post_format_content();

                            if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
                                if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
                                    printf(
                                        '<div class="et_main_video_container">
                                            %1$s
                                        </div>',
                                        et_core_esc_previously( $first_video )
                                    );
                                elseif ( ! in_array( $post_format, array( 'gallery' ) ) && 'on' === et_get_option( 'divi_thumbnails_index', 'on' ) && '' !== $thumb ) : ?>
                                    <a class="entry-featured-image-url" href="<?php the_permalink(); ?>">
                                        <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                                    </a>
                            <?php
                                elseif ( 'gallery' === $post_format ) :
                                    et_pb_gallery_images();
                                endif;
                            } ?>

                        <?php if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) : ?>
                            <?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) : ?>
                                <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            <?php endif; ?>

                            <?php
                                et_divi_post_meta();

                                if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
                                    truncate_post( 270 );
                                } else {
                                    the_content();
                                }
                            ?>
                        <?php endif; ?>

                            </article>
                    <?php
                        endwhile;

                        if ( function_exists( 'wp_pagenavi' ) )
                            wp_pagenavi();
                        else
                            get_template_part( 'includes/navigation', 'index' );
                    else :
                        get_template_part( 'includes/no-results', 'index' );
                    endif;
                ?>
            </div>

            <?php get_sidebar(); ?>
        </div>
    </div>
</div>

<?php

get_footer();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:10:24