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

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

相关文章:

  • 算法中的数学:费马小定理
  • python下通过wmic设置程序的优先级~~~
  • 如何理解线性判别分析(LDA)算法?
  • Java语言快速排序和堆排序(优先队列)介绍,附demo代码
  • 安卓11 多任务视图270 度的情况报错
  • 12软件测试需求分析案例-删除学生信息
  • 免费PDF工具-PDF24V9.16.0【win7专用版】
  • 2025年八大员【标准员】考试题库及答案
  • 基于netty实现视频流式传输和多线程传输
  • 25年软考架构师真题(回忆更新中)
  • 【检索增强生成(RAG)全解析】从理论到工业级实践
  • 动静态库--
  • 嵌入式<style>设计模式
  • 设计模式 - 模板方法模式
  • 前端项目部署 打包上线
  • Vue项目前后端交互大量数据的-之-前端优化方案
  • Java——设计模式(Design Pattern)
  • LINQ性能优化终极指南
  • 数据库中表的设计规范
  • S32K开发环境搭建详细教程(二、添加S32K3xx SDK)
  • 建网站需要什么程序/seo网站优化教程
  • 三乡有做网站的师傅吗/国内免费ip地址
  • 黄冈市住房和城乡建设厅网站/武汉seo搜索引擎
  • 铁岭市住房和城乡建设委员会网站/数字营销是干啥的
  • 网站运营专员做六休一/seo优化推广
  • 定制营销的概念与方法/seo公司优化方案