What is a child theme in WordPress and why do we need to use that?
Although we have a lot of prebuilt WordPress themes which we can use to design beautiful websites. But most of the time we need some modification in design or changes in the functionality of our website. We can do that by modifying our theme’s files like function.php, single.php, archive.php, style.css and other files vary as per theme.
Sounds ok, but what if we need to update our theme which is usually we have to do after some time. Theme updation will override our custom changes. To prevent this we need to create a child theme which just inherits the features of the parent theme.
Parent theme is the theme which you are using primarily and which you just install and update. You don’t need to change anything into the parent theme. If you need modification or any functionality changes you can achieve that by using a child theme.
Important: It is important to know that the parent theme consists of all files that are required by the theme but the child theme will only need to include the files where we want to customize anything.
So, without extra blah, blah let’s get started
It is very easy to start with a child theme in WordPress. Because you can create a child theme only with three things: a child theme directory or a folder where all files will be saved, style.css and function.php.
So, as I stated that first thing is directory, to do that follow the below steps
How’s it sound?
Now the next step is to create the style.css file in the child theme directory. To do that follow the below steps,
/* Theme Name: Twenty Twenty Child Theme URL: http://example.com Description: Twenty Twenty Child Theme Author: author name Author URL: http://authordomain.com Template: twentytwenty Version: 1.0.0 Text Domain: twentytwenty-child */
After the stylesheet, function.php is the last required file that you have to create. After this file you can create a file where you want to modify something otherwise your child theme is ready.
<?php add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 11 ); function my_theme_enqueue_styles() { wp_enqueue_style( 'child-style', get_stylesheet_uri() ); } ?>
Stylesheet loading is now recommended using hooks in function.php, if you are using @import in the style.css then it can slow loading of css and also possibly load the same file multiple times.
That’s it, these two files are the only required files to create the basic child theme. WordPress uses the files from the parent theme which is not present in the child theme. To understand this system, how WordPress uses the files if one is not present, you must understand the template hierarchy.
Though, you can play with code without reading about template hierarchy but if you understand this it will help you a lot to work with WordPress template structure.
The last thing but optional, add a screenshot of the parent theme to the child theme’s directory. So, follow the below steps,
Now when you are done with the above things, follow the below steps to activate the child theme.
That’s it, now you are ready with your child theme.
It’s time to know the basics of child theme customization. WordPress tries to find the required file in the child theme first and if not found then it will find it in the main or parent theme.
That means, if you want to customize any file from the parent theme you have to create that file into the child theme directory but with the same directory structure.
For an example,
If you want to edit the content files in the website then you have to follow the below steps,
Though we can add code to the head tag or before closing the body tag via function.php without using header.php and footer.php file. For that we need to use “wp_head” and “wp_footer” hooks.
Now, we know that,
What is a child theme in WordPress?
Why need to use a child theme in WordPress?
How to create a child theme in WordPress?
Activate the child theme in WordPress.
How to customize WordPress website with child theme?
If you have any query, please leave a comment.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…