在使用的时候,当我们想使用某个从网上下载的模板,但是又想做一些细微修改的情况下,我们可以制作一个子模板并启用它,子主题是可以基于这个主题,继承它的功能和样式,然后进行简单的代码编写就可以衍生出的一个新主题。子主题允许对父主题的功能进行修改,或者添加新功能。也就是说,你可以通过新建一个主题来修改另外一个主题。这样既可以节省开发时间,又可以在下次升级父主题的时候不会覆盖掉我们所做的修改。
在子主题中,一般有下面几个文件:
- style.css (必须有)
- functions.php (可以有)
- 其他模板文件 (可以有)
- 其他文件 (可以有)
下面我们以创建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-mou
SEOver" style=" margin-top: 12px; margin-b
ottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mou
SEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-title">style.cssCrayon-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">CSSCrayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
.
Crayon-line" id="
Crayon-5c891d1f793e5351813318-5">
Crayon-c">Author: 91wordpressCrayon-line
Crayon-striped-line" id="
Crayon-5c891d1f793e5351813318-6">
Crayon-c">Version: 1.0.0Crayon-line" id="
Crayon-5c891d1f793e5351813318-7">
Crayon-c">Template: twentyfifteenCrayon-line
Crayon-striped-line" id="
Crayon-5c891d1f793e5351813318-8">
Crayon-c">Tags: simpleCrayon-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-mou
SEOver" style=" margin-top: 12px; margin-b
ottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mou
SEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-title">functions.PHPCrayon-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">PHPCrayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
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">PHPCrayon-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() 函数可以指向子主题目录位置,这样可以引用子主题目录里面的其他文件。
赶紧动手制作一款子主题吧。
原文地址:https://www.jb51.cc/wordpress/238463.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。