WordPress建站教程:使用子主题Child Themes功能快速制作自己的主题

在使用的时候,当我们想使用某个从网上下载的模板,但是又想做一些细微修改的情况下,我们可以制作一个子模板并启用它,子主题是可以基于这个主题,继承它的功能和样式,然后进行简单的代码编写就可以衍生出的一个新主题。子主题允许对父主题的功能进行修改,或者添加新功能。也就是说,你可以通过新建一个主题来修改另外一个主题。这样既可以节省开发时间,又可以在下次升级父主题的时候不会覆盖掉我们所做的修改。

在子主题中,一般有下面几个文件:

下面我们以创建twentytwelve的子主题为例来写一篇

1. 新建一个文件夹,文件夹的名字可以任意起,比如91wordpress。在91wordpress文件夹中,新建一个样式文件style.css,键入以下代码

Crayon-5c891d1f793e5351813318" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">style.css
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">CSS
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
.
Crayon-line" id="Crayon-5c891d1f793e5351813318-5">Crayon-c">Author: 91wordpress
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793e5351813318-6">Crayon-c">Version: 1.0.0
Crayon-line" id="Crayon-5c891d1f793e5351813318-7">Crayon-c">Template: twentyfifteen
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793e5351813318-8">Crayon-c">Tags: simple
Crayon-line" id="Crayon-5c891d1f793e5351813318-9">Crayon-c">*/
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793e5351813318-10">Crayon-n">@import url("../twentytwelve/style.css");

解释:前面是一段 CSS 注释,会被 wordpress 系统 识别,必须放在 style.css 的最顶端。这段注释里面包含了 主题名称主题地址、描述、作者、作者名称、模板(父主题)、主题版本号等信息。在这些参数里面,只有 Theme Name 和 Template 是必须有的。其中 Template 参数的作用是指定父主题,只有在子主题中使用,在一般的主题中,是没有 Template 这个参数的。后面是使用 @import 导入命令,利用相对目录结构,把父主题的 CSS 文件导入到子主题中,如果父主题中有很多 css 文件,可以多写几行 @import 进行导入。

2. 在91wordpress文件夹中新建functions.PHP文件在这里你可以定义子主题的一些新功能。比如给主题添加favicon,可以使用下面的代码

Crayon-5c891d1f793ef440249314" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d1f793ef440249314-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d1f793ef440249314-2">2
Crayon-num" data-line="Crayon-5c891d1f793ef440249314-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d1f793ef440249314-4">4
Crayon-num" data-line="Crayon-5c891d1f793ef440249314-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d1f793ef440249314-6">6
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d1f793ef440249314-1">Crayon-ta">PHP
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793ef440249314-2">Crayon-t">functionCrayon-h"> Crayon-e">favicon_linkCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d1f793ef440249314-3">Crayon-h">Crayon-k ">echoCrayon-h"> Crayon-s">''Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">"\n"Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793ef440249314-4">Crayon-sy">}
Crayon-line" id="Crayon-5c891d1f793ef440249314-5">Crayon-e">add_actionCrayon-sy">(Crayon-s">'wp_head'Crayon-sy">,Crayon-h"> Crayon-s">'favicon_link'Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d1f793ef440249314-6">

3. 一些其他的文件,想修改哪些文件可以把这些文件从父主题里拷贝到子主题里,进行修改

友情提示:父主题的 style.css 文件已经被子主题的 style.css 文件替换了,子主题 style.css 文件在子主题目录中,使用 get_stylesheet_directory() 函数可以指向子主题目录位置,这样可以引用子主题目录里面的其他文件

赶紧动手制作一款子主题吧。

相关文章

我们有时候在定制WORDPRESS主题的时候,由于菜单样式的要求我...
很多朋友在做wordpree主题制作的时候会经常遇到一个问题,那...
wordpress后台的模块很多,但并不是每个都经常用到。介绍几段...
从WordPress4.2版本开始,如果我们在MYSQL5.1版本数据中导出...
很多网友会遇到这样一个问题,就是WordPress网站上传图片、附...
对于经常要在文章中出现代码的IT相关博客,安装一个代码高亮...