最近使用PHP做一个CMS,原先用的Joomla,在分页上一直没找到相关的API,无奈改用WordPress,记录一些资料,供日后查询。
 
刚接触WP的时候,一看模板很乱,底下有index.php,home.php,page.php等n多个文件,WP模板基本是这样的:
 
style.CSS                样式表文件
index.php                主页文件
single.php             日志单页文件
page.php                 页面文件
archvie.php            分类和日期存档页文件
searchform.php     搜索表单文件
search.php             搜索页面文件
comments.php         留言区域文件(包括留言列表和留言框)
404.php 404            错误页面
header.php             网页头部文件
sidebar.php            网页侧边栏文件
footer.php             网页底部文件
 
 
 
WP模板里面,有一些比较固定的函数。
             日志内容
  if(have_posts()) : ?>         确认是否有日志
while(have_posts()) : the_post(); ?> 如果有,则显示全部日志
      结束PHP函数” while
结束PHP函数” if
           显示格式为”02-19-08″的日期

              调用header.php文件的内容
              调用sidebar.php文件的内容
                调用footer.php文件的内容
显示一篇日志的留言链接
 

显示一篇日志或页面的标题
显示一篇日志或页面的永久链接/URL地址
显示一篇日志或页面的所属分类
显示一篇日志或页面的作者
显示一篇日志或页面的ID
显示一篇日志或页面的编辑链接
显示Blogroll中的链接
comments.php文件的内容
显示一份博客的页面列表
显示一份博客的分类列表
下一篇日志的URL地址
上一篇日志的URL地址
调用日历
显示一份博客的日期存档列表
显示较新日志链接(上一页)和较旧日志链接(下一页)
显示博客的描述信息
 
 
 
以上这些文件又如何定义呢?
可查看这篇文章:
WP的模板层次: http://codex.wordpress.org/Template_Hierarchy
 
 
大概意思是说,WP先使用条件标记判断当前请求的页面类型,根据下图规则查找匹配的页面,直至index.php,从而进行显示。
 
WP提供了一个静态页面的显示,因此,当用户请求一个URL时,WP先检查是否存在同名的静态页面。
 
解释如下:
WP->页面->添加新页面:home,那么当用户请求的URL为: http://localhost/home/时,wp先找模板中是否存在home.php,有,则调用显示,否则进入index.php进行显示
 
而非静态页面时,先查找文章、分类,并根据规则控制输出。
恩,理解起来有点拗口,看图直接点,最好还是看原文,以免误人子弟。
 
 
 
 
 
 
 
加载的顺序如下:
 
Home Page display    
1.home.php    
2.index.php 
   
Single Post display    
1.single.php    
2.index.php 
   
Page display    
WordPress Pages:
    
1.custom template - Where custom template is the Page Template assigned to the Page.    
2.page-{slug}.php - If the page slug is recent-news, WordPress will look to use page-recent-news.php    
3.page-{id}.php - If the page ID is 6, WordPress will look to use page-6.php    
4.page.php    
5.index.php 
   
Category display    
Category Templates:
    
1.category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php    
2.category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php    
3.category.php    
4.archive.php    
5.index.php 
   
Tag display    
Tag Templates:
    
1.tag-{slug}.php - If the tag's slug were sometag, WordPress would look for tag-sometag.php    
2.tag-{id}.php - If the tag's ID were 6, WordPress would look for tag-6.php    
3.tag.php    
4.archive.php    
5.index.php  
  
Author display    
Author Templates:
    
1.author-{nicename}.php - If the author's nice name were rami, WordPress would look for author-rami.php    
2.author-{id}.php - If the author's ID were 6, WordPress would look for author-6.php    
3.author.php    
4.archive.php    
5.index.php 
   
Date display    
Archive (Date) Templates:
    
1.date.php    
2.archive.php    
3.index.php   
 
Search Result display    
Creating a Search Page:
    
1.search.php    
2.index.php    

404 (Not Found) display    
Creating an Error 404 Page:
    
1.404.php    
2.index.php    

Attachment display    
Attachment Templates:
    
1.MIME_type.php - it can be any MIME type (p_w_picpath.php, video.php, audio.php, application.php or any other).    
2.p_w_upload.php    
3.single.php    
4.index.php