基础原理:WordPress如何找到首页?

WordPress的首页并不是一个固定的 index.html 文件,它的工作流程如下:

  1. 设置决定模板:你在WordPress后台的 “设置” > “阅读” 中,会看到一个“首页显示”的选项。

    • 您的最新文章:这是默认选项,WordPress会寻找一个名为 home.php 的模板文件来显示首页。home.php 不存在,它会继续寻找 index.php
    • 一个静态页面:如果你选择这个选项,就需要分别指定“首页”和“文章页”,WordPress会寻找名为 front-page.php 的模板文件来显示你指定的首页。front-page.php 不存在,它会寻找 home.php,再找不到就用 index.php
  2. 模板层级:理解WordPress的“模板层级”(Template Hierarchy)是关键,它会按照一个优先级顺序来查找模板文件:

    • 首选front-page.php (当你设置了“静态首页”时)
    • 次选home.php (当你设置了“最新文章”或静态首页但front-page.php不存在时)
    • 最终选择index.php (如果以上两个都不存在,这是所有WordPress主题的最终兜底模板)

要自定义首页,最佳实践是创建一个 front-page.php 文件,这样无论你后台如何设置,它都能作为首页的专用模板,并且不会影响你的文章列表页面(文章列表页面通常由 archive.phpindex.php 控制)。


如何构建首页模板(两种主流方法)

构建首页主要有两种方法:使用WordPress编辑器(无代码/低代码)直接编辑模板文件(代码)

使用WordPress编辑器(推荐新手)

这是最简单、最安全的方法,无需编写任何PHP代码,你的主题需要支持“区块编辑器”(Gutenberg)或“页面构建器”(如Elementor, Divi等)。

  1. 创建一个“首页”静态页面

    • 在后台 “页面” > “添加新”
    • 设为“首页”。
    • “属性” 面板中,将 “模板” 设置为你希望首页使用的模板(如果主题提供)。
    • “设置” > “阅读” 中,选择“一个静态页面”,并将“首页”设置为刚才创建的页面。
  2. 使用区块编辑器设计首页

    • 在“首页”页面编辑器中,你可以像搭积木一样添加各种区块:
      • 标题区块:用于添加网站主标题和副标题。
      • 图片区块:添加Logo、横幅图等。
      • 按钮区块:添加“了解更多”、“立即购买”等行动号召按钮。
      • 查询区块这是最强大的功能! 你可以使用“查询”区块来动态显示文章列表,最新文章”、“热门文章”或某个分类下的文章。
      • 媒体与文本区块:图文混排。
      • 区块模板:很多主题提供预设的首页区块模板,一键即可应用复杂的布局。
  3. 使用页面构建器

    如果你安装了Elementor、Divi等页面构建器插件,它们会提供更强大的拖拽式设计功能,可以创建出高度定制、视觉效果丰富的首页,并且操作直观。

优点:无需代码,可视化操作,安全,易于修改。 缺点:依赖主题和插件,可能不如代码灵活,加载速度可能稍慢。


直接编辑模板文件(推荐开发者/进阶用户)

这种方法需要你具备基本的PHP和HTML知识,并且通过FTP或主机文件管理器访问你的主题文件。

核心步骤:

  1. 创建 front-page.php 文件

    • 复制你主题根目录下的 index.php 文件。
    • 将复制后的文件重命名为 front-page.php
    • 你就有了一个可以安全修改的首页模板。
  2. 使用WordPress循环(The Loop)The Loop 是WordPress的核心,它负责获取并显示文章,一个基本的首页循环如下:

    <?php
    if ( have_posts() ) :
        while ( have_posts() ) : the_post();
            // 在这里显示每一篇文章的内容
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="entry-content">
                    <?php the_excerpt(); // 显示文章摘要 ?>
                </div>
            </article>
            <?php
        endwhile;
    else :
        // 如果没有文章,显示这条信息
        ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php
    endif;
    ?>
  3. 添加自定义内容: 你可以在循环之前或之后添加任何HTML结构,

    <?php get_header(); // 加载头部文件 header.php ?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
            <!-- 这里是首页的特有内容,比如一个大的横幅 -->
            <section class="hero-section">
                <h1>欢迎来到我的网站</h1>
                <p>这里是您的副标题或介绍语。</p>
                <a href="#" class="button">了解更多</a>
            </section>
            <!-- 这里是文章列表部分 -->
            <?php if ( have_posts() ) : ?>
                <div class="post-grid">
                    <?php while ( have_posts() ) : the_post(); ?>
                        <div class="post-card">
                            <?php if ( has_post_thumbnail() ) : ?>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail('medium'); // 显示特色图片 ?>
                                </a>
                            <?php endif; ?>
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <div class="post-meta">
                                <?php echo get_the_date(); ?> by <?php the_author(); ?>
                            </div>
                            <p><?php the_excerpt(); ?></p>
                        </div>
                    <?php endwhile; ?>
                </div>
                <!-- 分页导航 -->
                <div class="pagination">
                    <?php the_posts_pagination(); ?>
                </div>
            <?php else : ?>
                <p><?php _e( '没有找到任何内容。' ); ?></p>
            <?php endif; ?>
        </main><!-- #main -->
    </div><!-- #primary -->
    <?php get_sidebar(); // 加载侧边栏文件 sidebar.php ?>
    <?php get_footer(); // 加载底部文件 footer.php ?>

代码解释

  • get_header(), get_sidebar(), get_footer():加载主题的对应部分,这是WordPress模板的标准做法,便于维护。
  • have_posts() & the_post():检查并设置当前要显示的文章。
  • the_permalink():获取文章的链接。
  • the_title():获取文章标题。
  • the_excerpt():获取文章摘要。
  • the_post_thumbnail():获取文章的特色图片。
  • the_posts_pagination():生成标准的分页导航。

完整的 front-page.php 代码示例

这是一个结合了静态内容和动态文章列表的现代化首页模板示例。

<?php
/**
 * 首页模板文件
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package YourThemeName
 */
get_header();
?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main">
            <!-- Hero Section with a static title and CTA -->
            <section class="hero-section">
                <div class="container">
                    <h1><?php echo esc_html( get_bloginfo( 'name' ) ); ?></h1>
                    <p><?php echo esc_html( get_bloginfo( 'description' ) ); ?></p>
                    <a href="<?php echo esc_url( get_permalink( get_page_by_path( 'about-us' ) ) ); ?>" class="button">了解更多关于我们</a>
                </div>
            </section>
            <!-- Featured Posts Section -->
            <section class="featured-posts">
                <div class="container">
                    <h2>精选文章</h2>
                    <div class="post-grid">
                        <?php
                        // 创建一个新的WP_Query来获取精选文章
                        $featured_args = array(
                            'posts_per_page' => 3, // 显示3篇文章
                            'post_status'    => 'publish',
                            'orderby'        => 'date', // 按日期排序
                            'order'          => 'DESC',
                        );
                        $featured_query = new WP_Query( $featured_args );
                        if ( $featured_query->have_posts() ) :
                            while ( $featured_query->have_posts() ) : $featured_query->the_post();
                                ?>
                                <article class="featured-post">
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <a href="<?php the_permalink(); ?>" class="post-thumbnail">
                                            <?php the_post_thumbnail( 'large' ); ?>
                                        </a>
                                    <?php endif; ?>
                                    <header class="entry-header">
                                        <h3 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                        <div class="entry-meta">
                                            <?php
                                            echo esc_html( get_the_date() );
                                            echo ' | by ';
                                            the_author_posts_link();
                                            ?>
                                        </div>
                                    </header>
                                    <div class="entry-summary">
                                        <?php the_excerpt(); ?>
                                    </div>
                                </article>
                                <?php
                            endwhile;
                            wp_reset_postdata(); // 重置查询数据
                        else :
                            // 如果没有精选文章,显示提示
                            ?>
                            <p><?php _e( '没有找到精选文章。', 'yourthemename' ); ?></p>
                            <?php
                        endif;
                        ?>
                    </div>
                </div>
            </section>
            <!-- Latest Blog Posts Section -->
            <section class="latest-posts">
                <div class="container">
                    <h2>最新博客</h2>
                    <?php
                    // 使用主查询来显示最新的文章列表
                    if ( have_posts() ) :
                        while ( have_posts() ) : the_post();
                            get_template_part( 'template-parts/content/content', 'excerpt' ); // 加载一个专门的文章摘要模板
                        endwhile;
                        the_posts_navigation(); // 更现代的分页导航
                    else :
                        get_template_part( 'template-parts/content/content', 'none' ); // 加载“没有内容”的模板
                    endif;
                    ?>
                </div>
            </section>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
get_sidebar();
get_footer();

总结与建议

方法 优点 缺点 适合人群
WordPress编辑器 无需代码、可视化、安全、易于上手 灵活性受限、依赖主题/插件、可能影响性能 新手、非开发者、快速建站
编辑模板文件 完全控制、性能高、功能强大、代码纯净 需要PHP/HTML知识、有修改错的风险、更新主题可能覆盖修改 开发者、高级用户、追求极致性能和定制化的网站

给你的建议

  1. 新手:从 WordPress编辑器 开始,熟悉区块和查询功能,这足以应对90%的首页需求。
  2. 进阶用户/开发者:学习 front-page.php 和 The Loop,这是WordPress开发的基石,能让你构建出任何你想要的复杂布局,学习使用 get_template_part() 来拆分模板,让代码更整洁、可复用。