◈ 前言
WordPress是一款不错的建站系统,它的强大之处或许就是所谓的“臃肿”;与轻量级的博客系统相比,它的确显得“臃肿”不堪。然而,仔细看来,只是相对复杂而全面一些。对它知之甚少的我,不得不查阅一些相关资料。鉴于此,写了这一篇文章;或者说是,收集整理出了这一篇文章,也是为了更好、更方便、更快捷地学习并运用它。
◈ 目录结构
css:这是存放主题模板所需的样式表文件的目录,可以是statics(静态文件目录)或libs(库文件目录)的子目录。
js:这是存放主题模板所需的脚本文件的目录,可以是statics(静态文件目录)或libs(库文件目录)的子目录。
images:这是存放主题模板所需的图片文件的目录,可以是statics(静态文件目录)或libs(库文件目录)的子目录。
genericons:这是Genericons图标字体的目录,可以是fonts(字体目录)或statics(静态文件目录)或libs(库文件目录)的子目录。Genericons是针对博客的免费图标字体,由WordPress的母公司Automattic开发的一个Icon Font项目。
inc:一般指includes,是包含特殊PHP程序文件的目录,可以存放一些自定义的功能函数,例如template-tags.php等。
languages:这是主题翻译语言目录,包括.pot、.po、.mo等格式的文件,例如zh_CN.po、zh_CN.mo等。
template-parts:这是主题模板的部分或分模板目录,例如content.php、content-single.php等。
page-templates:这是主题模板的页面模板目录,例如full-width.php、contributors.php等。
widgets:这是主题模板所需的Widgets(挂件)目录,例如video-widget.php、dribbble-widget.php等。
◈ 模板文件
style.css:主题模板必须的样式表
index.php:主题模板必须的index文件。
single.php:主题的文章页面模板文件。
page.php:主题的page页模板文件。
author.php:主题的作者页面模板文件。
archive.php:主题的归档页面模板文件。
category.php:主题的分类页面模板文件。
tag.php:主题的标签页面模板文件。
searchform.php:主题的搜索表单模板文件。
search.php:主题的搜索页面模板文件。
image.php:主题的图片页面模板文件。
attachment.php:主题的附件页面模板文件。
comments.php:主题的评论页面模板文件。
404.php:主题的404错误页面模板文件。
header.php:主题的头部模板文件。
sidebar.php:主题的侧边模板文件。
footer.php:主题的底部模板文件。
functions.php:主题的功能函数文件。
screenshot.png:主题的快照图片文件。
<user-defined>.php:主题的自定义页面模板文件。
◈ 函数代码
◇ get_header
函数:get_header()、get_header($name)
简述:引入header.php模版文件
位置:wp-includes/general-template.php
定义:
/**
* Load header template.
*
* Includes the header template for a theme or if a name is specified then a
* specialised header will be included.
*
* For the parameter, if the file is called "header-special.php" then specify
* "special".
*
* @since 1.5.0
*
* @param string $name The name of the specialised header.
*/
function get_header( $name = null ) {
/**
* Fires before the header template file is loaded.
*
* The hook allows a specific header template file to be used in place of the
* default header template file. If your file is called header-new.php,
* you would specify the filename in the hook as get_header( 'new' ).
*
* @since 2.1.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific header file to use.
*/
do_action( 'get_header', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "header-{$name}.php";
$templates[] = 'header.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}
◇ get_footer
函数:get_footer()、get_footer($name)
简述:引入footer.php模版文件
位置:wp-includes/general-template.php
定义:
/**
* Load footer template.
*
* Includes the footer template for a theme or if a name is specified then a
* specialised footer will be included.
*
* For the parameter, if the file is called "footer-special.php" then specify
* "special".
*
* @since 1.5.0
*
* @param string $name The name of the specialised footer.
*/
function get_footer( $name = null ) {
/**
* Fires before the footer template file is loaded.
*
* The hook allows a specific footer template file to be used in place of the
* default footer template file. If your file is called footer-new.php,
* you would specify the filename in the hook as get_footer( 'new' ).
*
* @since 2.1.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific footer file to use.
*/
do_action( 'get_footer', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "footer-{$name}.php";
$templates[] = 'footer.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/footer.php');
}
◇ get_sidebar
函数:get_sidebar()、get_sidebar($name)
简述:引入sidebar.php模版文件
位置:wp-includes/general-template.php
定义:
/**
* Load sidebar template.
*
* Includes the sidebar template for a theme or if a name is specified then a
* specialised sidebar will be included.
*
* For the parameter, if the file is called "sidebar-special.php" then specify
* "special".
*
* @since 1.5.0
*
* @param string $name The name of the specialised sidebar.
*/
function get_sidebar( $name = null ) {
/**
* Fires before the sidebar template file is loaded.
*
* The hook allows a specific sidebar template file to be used in place of the
* default sidebar template file. If your file is called sidebar-new.php,
* you would specify the filename in the hook as get_sidebar( 'new' ).
*
* @since 2.2.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific sidebar file to use.
*/
do_action( 'get_sidebar', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "sidebar-{$name}.php";
$templates[] = 'sidebar.php';
// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/sidebar.php');
}
◇ get_template_part
函数:get_template_part($slug, $name)
简述:引入其他模版文件
位置:wp-includes/general-template.php
定义:
/**
* Load a template part into a template
*
* Makes it easy for a theme to reuse sections of code in a easy to overload way
* for child themes.
*
* Includes the named template part for a theme or if a name is specified then a
* specialised part will be included. If the theme contains no {slug}.php file
* then no template will be included.
*
* The template is included using require, not require_once, so you may include the
* same template part multiple times.
*
* For the $name parameter, if the file is called "{slug}-special.php" then specify
* "special".
*
* @since 3.0.0
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
*/
function get_template_part( $slug, $name = null ) {
/**
* Fires before the specified template part file is loaded.
*
* The dynamic portion of the hook name, `$slug`, refers to the slug name
* for the generic template part.
*
* @since 3.0.0
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialized template.
*/
do_action( "get_template_part_{$slug}", $slug, $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "{$slug}-{$name}.php";
$templates[] = "{$slug}.php";
locate_template($templates, true, false);
}