在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>© <?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"><?phpprintf(_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"><?phpwp_list_comments(array('callback' => 'custom_comment_callback','style' => 'ol','short_ping' => true,));?></ol><?phpthe_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; ?><?phpif (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) :?><p class="no-comments"><?php _e('Comments are closed.', 'textdomain'); ?></p><?phpendif;comment_form();?>
</div>
這些模板文件相互配合,共同構成了WordPress主題的完整結構。通過合理地編寫和使用這些模板文件,可以實現豐富多樣的網站布局和功能。
原文
https://www.jianzhanpress.com/?p=8574