Home > CMS Tutorial > WordPress > body text

How to add a wordpress child theme

Release: 2019-07-27 16:34:03
Original
3719 people have browsed it

How to add a wordpress child theme

Introduction and principle of WordPress child theme

WordPress provides the child theme function, allowing us to use child themes Modify the WordPress theme without directly modifying the source code, and content that cannot be updated appears. A child theme needs to have a parent theme to be enabled. According to the main file of the WordPress theme, if the child theme exists, WordPress will automatically use the file of the child theme and abandon the use of the parent theme.

In this way, which file of the parent theme we want to modify first, we can copy the file to the child theme, and then modify the file, so that the purpose of modification can be achieved. Of course, only WordPress theme core files can be identified and replaced, and most themes have many other files that cannot be modified in addition to the main files.

1. Why use subthemes?

Every time you use a theme, there may be some changes that may need to be made to suit your needs. For example, some are blog themes and want to be turned into CMS magazine themes. Some links in the topic, such as the irrelevant links at the bottom, must be changed if you want to remove the link. (If the author links to the topic, it is recommended to keep the source and use it for free. Keeping the link is the greatest respect.) In the process of modifying the original topic countless times, you will suddenly forget what you changed. Once you make a mistake, you have to Again. Or, once the author of the original theme updates and upgrades, and you need to update and upgrade, you find that you may need to modify it again. In this way, it becomes very troublesome. I want to upgrade but don’t want to bother. I don’t want to upgrade, but I like some functions or UI changes of the new theme. How to do it? Using a WordPress sub-theme can reduce the worries of theme upgrades. If you make a mistake during the tossing process, you won't know which file to modify when you start over again. At the same time, it is also a good helper for you to create a theme for multi-language websites. Of course, if your theme is multi-lingual, you don’t need subthemes.

2. How to enable the sub-theme function?

The method is very simple. The creation method is as follows:

1. Create a new sub-theme directory in the same directory as the parent theme, that is, the original theme where the sub-theme needs to be created. For example, I use Begin The child theme created by the theme is Begin Child.

How to add a wordpress child theme

2. Create the theme file

style.css included in the theme in the child theme directory (required)

functions.php (optional)

Template files (optional)

Other files (optional)

screenshot files (optional)

3. Modify the child theme style file style.css to the following content:

/*

Theme Name: Child theme name such as: Begin Child

Theme URI: Theme home page

Author: Theme author

Author URI: Author theme

Description: Sub-theme description content

Template: Parent theme such as begin

Version: The theme version such as 1.8

*/

is simply explained as follows:

Theme Name. (Required) The name of the sub-theme.

Theme URI. (Optional) The child theme's home page.

Description. (Optional) Description of the subtopic. For example: My first sub-theme, awesome!

Author URI. (Optional) Author home page.

Author. (optional) The author's name.

Template. (Required) The directory name of the parent theme, case sensitive. Note: When you change the name of a subtheme, you must first change it to another theme.

Version. (Optional) The version of the child theme. For example: 0.1, 1.0, etc. After completing the above content, that is, the sub-theme is basically created, and the sub-theme can be enabled. At this time, the sub-theme is no different from the original theme, but there is no style.

3. Issues to note when using subthemes

1. After creating a subtheme, style.css cannot be empty and must have the style content of the original theme. You can directly copy style.css from the original parent theme to the child theme, and modify the header to the child theme information. Or directly import the parent theme style file: @import url("../Begin/style.css"); and then add other style content.

2. Parent theme functions cannot be used in the child theme’s theme function template functions.php. Functions that are not required by the parent theme can be removed. Because the functions.php function template exists in both the child theme and the parent theme, it will be introduced at the same time in the order of the child theme first and then the parent theme.

3. The child theme replaces some functions of the parent theme function template as follows:

if (!function_exists('theme_new_function')) {function theme_new_function() {// Function content.} }

4. Other theme function files will replace the parent theme template file with the same name. For example, if the parent theme homepage template is a blog template and is changed to a CMS magazine template in the child theme, you can create an index.php file under the child theme and modify it to a CMS magazine template style.

The above is basically how to use WordPress subtheme and some instructions. If you want a more in-depth understanding, you can check related articles on the WordPress official website.

For more wordpress-related technical articles, please visit the wordpress tutorial column to learn!

The above is the detailed content of How to add a wordpress child theme. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template