<>
Puede que te sientas tentado de ir y editar el source code de un tema de terceros (es decir, un tema que no hayas escrito tú mismo), pero no lo hagas La próxima vez que actualices ese tema, se descargarán nuevos archivos y perderás todos los cambios que hayas hecho.
Frustrating, to say the least.
But what can you do if you need to edit the theme?
Luckily there is a way to edit the code in a theme, and still have the ability to download the latest updates: that is by creating a child theme using the original theme as the main theme.
Esto significa que activará el tema hijo en el administrador de WordPress, pero dentro del código de ese tema hay instrucciones para que WordPress le diga que este es un tema hijo, y cuál es el tema padre. Usted mantiene el tema padre instalado en su sitio, pero no lo activa. (Es esencial mantenerlo instalado, dado que sin él, el tema infantil se romperá.)
En este tutorial te voy a enseñar cómo crear un tema infantil en WordPress. Al mismo tiempo te daré algunos consejos sobre cómo funcionan los temas para niños y padres, para es importante es importante que sepas qué conjunto de código se está utilizando para mostrar el contents de tu sitio.
Creating the Child's Theme
The first thing to do is create the child theme in your wp-content / themesfolder folder. The child theme must have two files to work with: a style sheet and a function file. At the same time, you can add optional files, such as template files or include files.
Create a folder for your child's topic in the topic folder and give it a suitable name. I'm calling mine »My son theme».
Inside that folder create a file called style.css.
At the top of that file, add the following commented code:
This tells WordPress that this is a theme, and provides the same information on the theme that you would find in any theme - with one addition. Template: twenty-two lines
tells WordPress that it consists of a child from the Twenty Nineteen theme.
You can use any theme as the main theme, using the name of the folder where it is stored. Don't use the topic title - use the folder name instead.
Ahora guarde la style sheet y cree otro archivo en el tema hijo, llamado functions.php.
In this file, you need to glue the stylesheet of the main theme. It used to be that you did this using a line
@import in the stylesheet, but this is no longer the recommended way to do it.
Instead, in the function file, add this code:
<?php
function
pimpampress_parent_styles () {
wp_enqueue_style (
'parent'
, get_template_directory_uri ().
'/style.css'
);
}
add_action (
'wp_enqueue_scripts'
,
'pimpampress_parent_styles'
);
This correctly places the main theme stylesheet, using get_tempate_directory_uri() for
find that theme (the template directory is the folder where the main theme is stored, while the stylesheet directory is the folder where the current theme is stored).
If you want to add any styles to the child theme to override or increase the style of the parent theme, go back to the child theme's style sheet and add it there. Do not add in the functions file or try to queue in more stylesheets.
At the same time you can add functions to the functions file of the child theme, and template files to the themes folder, which will override the same template files in the main theme.
Therefore, now you have the topic of your child, it's that easy! However, it is important to note that you understand exactly how the parent and child theme template files interact.
Create template files
Now let's take a look at the theme template files. The template file WordPress uses to display a page on your site will depend on two things: the template hierarchy, and the files you add to the child theme.
Imagine you are viewing the category archive page Travels on your site. WordPress will use the template hierarchy to find the most relevant file:
- a category archive template file for that specific category, using the slug: category-travel.php
- a category archive template file for that specific category, using the ID: category-23.php
- a general category file: category.php
- a general archive file: archive.php
- el catch-all: index.php
WordPress buscará esto tanto en el tema para padres como en el tema para niños. WordPress utilizará el primer archivo de la jerarquía que encuentre, ya be en el tema padre o en el tema hijo.
There is an exception. When the most relevant template file has a version in the parent and child themes, WordPres will use the file from the child theme and ignore the file from the parent theme. This is one of the most common uses of child themes: overriding a specific file in the parent theme.
Here are some examples, using the example category above:
- If your child theme has archive.php e index.php, and its parent theme has category.php and index.php, then WordPress will use category.php of the parent theme because it is highest in the hierarchy
- If your child theme has category.php e index.php and its parent theme has file.php and index.php, WordPress will use the file category.php of the child theme, since this is the highest in the hierarchy
- If the child theme has archive.php e index.php and he parent theme has archive.php and index.php, WordPress will use the archive archive.php of the child theme. This is because there are two copies of the highest file in the hierarchy and the child topic takes precedence over the parent topic.
This way, if you want to override a template file in the parent theme, create a duplicate of that file in the child theme with whatever code you want. Or create a file that is higher in the template hierarchy and add it to your child's theme.
Adding functions to the child theme
The functions work differently from template files and are a bit less straightforward.
If you add a function to the child theme with the same name as one of the parent theme, WordPress will throw an error, because it is trying to call the same function twice.
But this won't happen if the function in the parent theme is pluggable.
A plugin file is included in a conditional check for another function with the same name, which looks like this:
1 2 3 4 5 6 7 8 |
|
Here, WordPress will check that no function with the same name has been called. If so, it will execute this function. This is because the functions of the child topic are called before the functions of the parent topic. If you write a function in the child theme with the same name, it will run instead.
But what if the parent theme's function is not pluggable and you want to override it?
Well, you can do that by writing a new function that essentially overrides the function in the parent theme, and giving it a higher priority than the function in the parent theme, so that the funciton function in the parent theme executes after it. Alternatively, you can pick up the function from the parent topic and write a new function on the child topic, hooked to that same hook.
Imagine that the main theme function looks like this:
function
pimpampress_parent_function () {
}
add_action (
'init'
,
'pimpampress_parent_function'
);
If your function in the child theme could undo the function of the parent theme without having to stop the function of the parent theme, you would write it like this:
function pimpampress_child_function () {
// contents of function here
}
add_action ('init', 'pimpampress_child_function', 20);
But if you had to prevent the parent theme's function from executing, you'd unhook it first, like this:
function wpmu_remove_parent_function () {
remove_action ('init', 'pimpampress_parent_function');
}
add_action ('wp_head', 'wpmu_remove_parent_function');
Note that you still have to put the function remove_action ()
inside another function, hooked to the wp_head () hook in this
case.
Then you could write the function for the child theme as if the one for the parent theme never existed, remember you shouldn't give it the same name!
Children's themes are a useful tool for editing themes
Children's themes have two main uses:
- With a theme that has been designed to be used as a parenting theme (often referred to as a framework). The framework is not designed for use on its own: instead, additional styles and template files are added with a child theme.
- To allow you to edit the main theme without having to edit it directly, or to add additional template files. This means that when the parenting topic is updated in the future, you won't lose your job.
So the next time you read a tutorial that tells you to use a children's theme instead of editing a third-party theme, you will know what to do!
2EwyhSO