wordpress主题开发中常用的12个模板文件

发布于:2025-05-25 ⋅ 阅读:(22) ⋅ 点赞:(0)

在WordPress主题开发中,有多种常用的模板文件,它们负责控制网站不同部分的显示内容和布局,以下是一些常见的模板文件:

1.index.php

这是WordPress主题的核心模板文件。当没有其他更具体的模板文件匹配当前页面时,WordPress就会使用index.php来显示内容。它通常用于显示博客的主页,展示文章列表等。例如,一个简单的index.php文件可能包含以下代码:

<?php get_header(); ?>
<div id="content">
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

2.header.php

用于定义网站的头部区域,通常包含网站的标题、导航菜单、logo等内容。例如:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php wp_title('|', true, 'right'); ?></title>
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id="header">
    <h1><?php bloginfo('name'); ?></h1>
    <div id="nav">
        <?php wp_nav_menu(array('theme_location' => 'primary')); ?>
    </div>
</div>

3.footer.php

定义网站的底部区域,通常包含版权信息、底部菜单等内容。例如:

<div id="footer">
    <p>&copy; <?php echo date('Y'); ?> <?php bloginfo('name'); ?>. All rights reserved.</p>
    <div id="footer-nav">
        <?php wp_nav_menu(array('theme_location' => 'footer')); ?>
    </div>
</div>
<?php wp_footer(); ?>
</body>
</html>

4.sidebar.php

用于定义侧边栏的内容,通常包含小工具(widgets)等。例如:

<div id="sidebar">
    <?php if (is_active_sidebar('sidebar-1')) : ?>
        <?php dynamic_sidebar('sidebar-1'); ?>
    <?php endif; ?>
</div>

5.single.php

用于显示单篇文章的完整内容。例如:

<?php get_header(); ?>
<div id="content">
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

6.page.php

用于显示单个页面的内容,比如“关于我们”“联系我们”等页面。例如:

<?php get_header(); ?>
<div id="content">
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="page">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

7.archive.php

用于显示文章归档页面,比如分类归档、标签归档、日期归档等。例如:

<?php get_header(); ?>
<div id="content">
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="page">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

8.category.php

用于显示特定分类的归档页面。如果存在category.php文件,WordPress会优先使用它来显示分类归档页面,而不是使用archive.php。例如:

<?php get_header(); ?>
<div id="content">
    <h1><?php single_cat_title(); ?></h1>
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

9.tag.php

用于显示特定标签的归档页面。如果存在tag.php文件,WordPress会优先使用它来显示标签归档页面,而不是使用archive.php。例如:

<?php get_header(); ?>
<div id="content">
    <h1><?php single_tag_title(); ?></h1>
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

10.search.php

用于显示搜索结果页面。例如:

<?php get_header(); ?>
<div id="content">
    <h1>Search Results</h1>
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="post">
                <h2><?php the_title(); ?></h2>
                <div class="entry">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else : ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

11.404.php

用于显示404错误页面,当用户访问不存在的页面时会显示该页面。例如:

<?php get_header(); ?>
<div id="content">
    <h1>404 Not Found</h1>
    <p>Sorry, the page you are looking for does not exist.</p>
</div>
<?php get_footer(); ?>

12.comments.php

用于显示文章或页面的评论区域。例如:

<?php if (post_password_required()) {
    return;
} ?>
<div id="comments" class="comments-area">
    <?php if (have_comments()) : ?>
        <h2 class="comments-title">
            <?php
            printf(_n('One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'textdomain'), number_format_i18n(get_comments_number()), '<span>' . get_the_title() . '</span>');
            ?>
        </h2>
        <ol class="comment-list">
            <?php
            wp_list_comments(array(
                'callback' => 'custom_comment_callback',
                'style' => 'ol',
                'short_ping' => true,
            ));
            ?>
        </ol>
        <?php
        the_comments_pagination(array(
            'prev_text' => '<span class="screen-reader-text">' . __('Previous', 'textdomain') . '</span>',
            'next_text' => '<span class="screen-reader-text">' . __('Next', 'textdomain') . '</span>',
        ));
        ?>
    <?php endif; ?>
    <?php
    if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) :
        ?>
        <p class="no-comments"><?php _e('Comments are closed.', 'textdomain'); ?></p>
    <?php
    endif;
    comment_form();
    ?>
</div>

这些模板文件相互配合,共同构成了WordPress主题的完整结构。通过合理地编写和使用这些模板文件,可以实现丰富多样的网站布局和功能。

原文

https://www.jianzhanpress.com/?p=8574


网站公告

今日签到

点亮在社区的每一天
去签到