当前位置: 首页 > article >正文

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

在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"><?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

http://www.lryc.cn/news/2385174.html

相关文章:

  • 聚铭安全管家平台2.0重磅发布——大模型智驱高效降本新方向
  • Android singleTop启动模式开启新页面
  • 使用注解动态映射:根据实体List列表动态生成Excel文件
  • 基于cornerstone3D的dicom影像浏览器 第二十一章 显示DICOM TAGS
  • 【循环位运算——uint32,DP】
  • 贪心介绍 LeetCode 455.分发饼干 LeetCode 376. 摆动序列 LeetCode 53. 最大子序和
  • 算法学习笔记·数学·快速幂
  • Postgresql 数据库体系架构
  • [创业之路-377]:企业战略管理案例分析-战略制定/设计-市场洞察“五看”:看宏观之社会发展趋势:数字化、智能化、个性化的趋势对初创公司的战略机会
  • Vue框架1(vue搭建方式1,vue指令,vue实例生命周期)
  • 分布式系统核心技术全解析
  • skywalking 10.2 源码编译
  • C++ --- string
  • Android Studio 连接夜神模拟器 自动断开的问题
  • Python入门手册:Python中的数据结构类型
  • 《P3435 [POI 2006] OKR-Periods of Words》
  • C/C++---隐式显式转换
  • 巡礼中国西极·跨越昆仑天山 | 北斗卫星徽章护航昆仑科考
  • Vue常用自定义指令-积累的魅力【VUE】
  • LangChain4j第三篇: RAG的简单应用与实践
  • 机器学习第二十六讲:官方示例 → 跟着菜谱学做经典菜肴
  • 功能强大且易于使用的 JavaScript 音频库howler.js 和AI里如何同时文字跟音频构思想法
  • 品鉴JS的魅力之防抖与节流【JS】
  • 如何使用patch-package给npm包打补丁
  • maxkey单点登录系统
  • windows bat 在目录下(包括子目录)搜索批量指定文件名称复制到另一个文件夹内
  • Notepad++ 下载与安装教程(小白专属)
  • Spring Cloud Gateway 微服务网关实战指南
  • 微服务架构实战:Eureka服务注册发现与Ribbon负载均衡详解
  • 采用多维计算策略(分子动力学模拟+机器学习),显著提升 α-半乳糖苷酶热稳定性