当前位置: 首页 > 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

http://www.xdnf.cn/news/622981.html

相关文章:

  • 黑马k8s(十五)
  • 【触想智能】什么是工控一体机,工控一体机有什么用途?
  • 前端框架6
  • 折半搜索【2024华为智联杯 K.时光】
  • 安卓无障碍脚本开发全教程
  • Android-Glide学习总结
  • 当AI Agent遇上聊天机器人:一场关于效率与能力的较量
  • Day 0017:Web漏洞扫描(OpenVAS)解析
  • 【Java学习笔记】代码块
  • Express笔记
  • 塔能节能平板灯:点亮苏州某零售工厂节能之路
  • Oracle表索引变为不可用状态了怎么办
  • UniApp === H5实现主题切换
  • 【检索增强生成(RAG)全解析】从理论到工业级实践
  • commonmark.js 源码阅读(二) - Inline Parser
  • leetcode 两两交换链表中的节点 java
  • 【R语言科研绘图】
  • 讯飞AI相关sdk集成springboot
  • Matlab实战训练项目推荐
  • LangGraph-agent-天气助手
  • 自然语言处理核心技术:词向量(Word Embedding)解析
  • 【读代码】BAGEL:统一多模态理解与生成的模型
  • 服务器硬盘虚拟卷的处理
  • 如何合法使用代理IP?
  • HTTP协议初认识、速了解
  • 奇好 PDF安全加密 + 自由拆分合并批量处理 OCR 识别
  • 记录python在excel中添加一列新的列
  • 【系统设计】2WTPS生产级数据处理系统设计Review
  • 大数据如何让智能物流和仓储管理更高效?从预测到自动调度
  • 【AI实战】从“苦AI”到“爽AI”:Magentic-UI 把“人类-多智能体协作”玩明白了!