Child theme doesn't read template part file - php

I'm building my first child theme. My child theme is my active theme at the moment. So, I have a file under the parent theme which is at wp-content/themes/mytheme/template-parts/ajax-mytick-list.php and I copied it to wp-content/themes/mytheme-child/template-parts/ajax-mytick-list.php and edit it there. But the site is not showing the one that I edited under child theme. Cos if I edit the same file in parent theme, the result is reflected.
What am I doing wrong?

You can override page templates this way, but not other files. In this case, you'll need to override the according function (guide) or hook into it with an action hook or filter.
More details according to your question below:
You'll need to find out where the template-parts file is getting included. Search the parent theme for something like
require get_template_directory() . '/template-parts/ajax-mytick-list.php';
If that call is in a template file, copy it to your child theme and change the call to
require get_stylesheet_directory() . '/template-parts/ajax-mytick-list.php';
If it's not in a page template, you'll need to dig deeper, but as every theme is different, it's impossible to say how exactly you'll get there.

Related

Will the header.php updated in child-theme when the theme is updated?

I have a theme installed and I want to add a header at the top of the existing code. So not to lose it when the theme is updated I'm thinking of creating a child theme and copy the ´header.php` and then edit it in the child theme folder.
So when the theme is updated with changes in the header.php from the main theme developer, will the file that I copied and changed in the child-theme folder have the updates with the code I added? Or it will remain the same whenever the theme is updated?
I mean to have the updates from the developer and my custom header remains there , So for example if he changed the order of the header elements it will be implemented to the child-theme and my custom header will remain?
Is there is a better way for adding that header without creating a child-theme?
Using a child theme is exactly what you need to do. Think of the files in the child theme as an override to what is in the parent theme. If you copy header.php from the parent theme into the child theme and change it, it will load the header.php from the child as opposed to the parent. When you update the parent, the child is untouched. What you're doing is the best way to make your edits without the main theme overwriting your changes.
If the main theme's header.php is altered by a theme update, the child theme's header.php won't be touched.
So if there are useful changes in the main theme's header.php which you want to become effective for your child theme, you'll have to transfer those parts of the code to your child theme's header file by editing it accordingly.

WordPress Child Theme - General Understanding

I am trying to understand how to use a Child Theme in wordpress.
I am afraid that something will go wrong in the process of developpment because I haven't completely understood how to use the child theme or its integration wasn't done properly.
My confusion comes after I create the child theme and by adding the style.css and functions.php.
At this point, can I make any changes that I want to the child theme and it will still work?
For example,
After I modify the front-end of the website with the use of the child theme, I want to add some forms that will insert data into the database.
Do I have to implement any php files from the parent-theme in order to make this work ?
Or do I treat my child-theme as a fresh theme (blank canvas) and code the back-end however I would like ?
Thanks
Think of the child theme as a layer on top of the parent theme. By default (if you add nothing to the child theme other than a style.css file), all the templates from the parent theme will be used. The only thing you have to have is the style.css file. If you wanted to override the page.php template for instance, you would simply just make a page.php file in your child directory and WordPress will use that template instead of the parent template. Same goes for any template file or template part.
You can have a functions.php file in your child theme and it will be included in addition to the parent functions file.
Usually the parent's style.css file would be enqueued before the child's style.css file, but it depends on how those files get enqueued. The system is quite flexible.
Your child theme is not a blank canvas. It inherits style.css, functions.php and all the post/page templates from the parent theme. The style.css file of your child theme will load after that of the parent theme, thus enabling you to add more CSS rules. The functions.php file of your child theme will load before the parent's functions.php file.
The child also inherits all the templates from the parent. However, if a template exists in the parent and you create it in the child the child theme's template will completely override the parent's template. For more information on parent-child theme relationship I highly recommend that you check the official child themes documentation.

Wordpress: editing child theme

I want to edit some stuff on my website. I'm using a child theme and I plan to change some CSS and do some structural changes. I know the CSS is easy to change, as I only need to call the names of the classes or IDs and give the new values.
But, if I want to change things around in html, do I need to copy the whole code? Will I lose every change when the theme updates (as in: i copied and pasted the older version and now I need to copy and paste the new version)?
I don't know much about the names of things and how to describe them properly.
Make copies of the theme files in the parent theme you want to modify - such as index.php, category.php, etc - and move those to the child theme and edit them. The copies in the child theme will be used by WordPress rather than the same-named files in the parent. This includes file in folders, i.e. /css/style.css, so duplicate the file structure in the child theme, if needed.
The child theme will continue to use those files, even if/when the parent theme get updated. And that also means your child theme edits won't disappear, unless there are major structural/functional changes to the parent theme.
One exception to child theme file usage is functions.php:
Unlike style.css, the functions.php of a child theme does not override
its counterpart from the parent. Instead, it is loaded in addition to
the parent’s functions.php. (Specifically, it is loaded right before
the parent’s file.)
See http://codex.wordpress.org/Child_Themes
Taken from Wordpress Doc - Child Themes:
If you want to change more than just the stylesheet, your child theme
can override any file in the parent theme: simply include a file of
the same name in the child theme directory, and it will override the
equivalent file in the parent theme directory when your site loads.
For instance, if you want to change the PHP code for the site header,
you can include a header.php in your child theme's directory, and that
file will be used instead of the parent theme's header.php.
Wordpress Child Theme can't have a child theme itself. It's not supported by Wordpress. I had the same problem before and after some searches I found this article.
It explains about creating a grandchild theme; from the article:
The solution is surprisingly simple. Instead of editing the child
theme, create a grandchild theme. It’s very similar to creating a
child theme, except you do it via a plugin. You add your custom
functions to the plugin, just as you normally would in functions.php
(though remember your plugin will be called much earlier than
functions.php, so you’ll need to make sure that any code in your
plugin only runs when an action is fired). Use the wp_register_style
and wp_enqueue_style functions to add your CSS (by default a
grandchild theme will add your CSS to the CSS of the parent or child
theme, but you can change this behaviour by using the wp_dequeue_style
function). Finally filter the result of the get_query_template
function to control which PHP files are executed when a page is
requested.
It's not really a child theme of a child theme, but a plugin including calls to add_filters or something similar to override the behaviour of the child theme. In this way upgrading child / parent themes will not affect the customisation you've made.

Using WP Child Theme to override a PHP file within sub directories of main WP theme

Here is the file hierarchy:
Main Theme>PageBuilder>Blocks>campaigns-block.php
I edited the campaigns-block.php to suit my needs & now want to place it in my Child Theme to override. The Main Theme's campaigns-block.php file.
How might I do this?
Unfortunately, in the child theme you can only automatically override parts of the Template Heirarchy (header.php, page.php, etc.) You would need to override the template file that includes campaigns-block.php and direct it to your copy in the child theme folder.
See this answer for more details:
https://wordpress.stackexchange.com/questions/62341/override-file-in-parent-theme

Should I use a wordpress hook to replace a file?

I'm having a little trouble deciding how I should be implementing hooks in wordpress.
I'm currently using a child theme and in the single.php file of the parent theme there is a function <?php con_get_sharebox(); ?> that gets the contents of a file sharebox.php.
Now the sharebox.php file is the file I'd like to edit but I don't know how to do this using hooks.
I tried simply adding a different sharebox.php to my child theme but because its not part of the standard theme files I don't think it gets loaded.
Any ideas on what I can do?

Categories