Wordpress Child Theme Includes Files Not Overriding Parent Version

Joined
May 14, 2022
Messages
75
Likes
54
Degree
0
I created a child theme & coppied all customisations. This child theme runs now (activated) and nothing has changed (as it should be).

Now I wanted to modify a file in the child theme (template-tags.php). I coppied (duplicated) the file to the child theme and made the adjustments. However, nothing changes...

When I edit the parent them exactly the same it does work.

Any clues?
 
Sorry another question from me. Trying to learn the ropes.

I created a child theme & coppied all customisations. This child theme runs now (activated) and nothing has changed (as it should be).

Now I wanted to modify a file in the child theme (template-tags.php). I coppied (duplicated) the file to the child theme and made the adjustments. However, nothing changes...

When I edit the parent them exactly the same it does work.

Any clues?
Struggled hours with this but no solution. I found out that you need to call the new file in the child functions.php. When I do this, I get an error: "Cannot redeclare X (previously declared in ".
So, I need to define which part to ignore? Realy stuck here.
 
Struggled hours with this but no solution. I found out that you need to call the new file in the child functions.php. When I do this, I get an error: "Cannot redeclare X (previously declared in ".
So, I need to define which part to ignore? Realy stuck here.
You shouldn't have to do anything of the sort. You should simply be placing the PHP template file in the exact same place in the theme folder hierarchy as it is in the parent theme.

So if your folder is /parent-theme/partials/template-tags.php then you need to create that same file path in your child theme, meaning /partials/template-tags.php.

The child theme system is designed to detect and override the parent theme files with the ones in the child theme. You don't need to explicitly tell it to do that. That's what it's there for.
 
You shouldn't have to do anything of the sort. You should simply be placing the PHP template file in the exact same place in the theme folder hierarchy as it is in the parent theme.

So if your folder is /parent-theme/partials/template-tags.php then you need to create that same file path in your child theme, meaning /partials/template-tags.php.

The child theme system is designed to detect and override the parent theme files with the ones in the child theme. You don't need to explicitly tell it to do that. That's what it's there for.
Thanks Ryuzaki for your reply. Thats what I thought. I did this and nothing happened. When I make the same adjustments in the parent template-tags.php it does work. Online I found similar cases and therefore came to the above possible solution.
 
can you upload a screenshot of the child theme folder structure? And the PHP code you are adding/changing
 
can you upload a screenshot of the child theme folder structure? And the PHP code you are adding/changing
When I place this in the parent it works.
I duplicated the template-tags.php file from parent to child and when I insert the code at the exact same spot it does not work (which probably means it does not look to the child template-tags but to the parent).

7zQKk2E.jpeg
 
Can you maybe do a temporary simple change of h1 color in the styles file of the child theme, just to ensure you have it set up properly?
 
Struggled hours with this but no solution. I found out that you need to call the new file in the child functions.php. When I do this, I get an error: "Cannot redeclare X (previously declared in ".
So, I need to define which part to ignore? Realy stuck here.
I think I understand your problem now. I'm guessing (and may be wrong) that when you're doing a require_once() that you might be using something like require_once(get_template_directory() . '/includes/template-tags.php');. Note the get_template_directory() part, which could be "TEMPLATEPATH" or something like that, too.

This is probably still referring to the parent theme's file location, which would be why you're getting the error that the file is already included.

The solution would be, in your child theme's functions.php file, to change how you're accessing the "template directory" (meaning the theme folder). In the child function.php file, when you require the new version of the template-tags.php file, try some variation of include( get_stylesheet_directory() . '/includes/template-tags.php'); instead (assuming your main stylesheet is in the root of the child theme folder).

What I'm hoping to achieve is to get it to call to the child theme folder instead of the parent theme folder.

If that fails, in your child functions.php try changing the "require_once" to get_template_part('includes/template-tags.php');. Please take note in all my examples that your file path may or may not have "includes" in it and could be some other sub-folder.
 
Thanks for all the help so far!

I think I understand your problem now. I'm guessing (and may be wrong) that when you're doing a require_once() that you might be using something like require_once(get_template_directory() . '/includes/template-tags.php');. Note the get_template_directory() part, which could be "TEMPLATEPATH" or something like that, too.

This is probably still referring to the parent theme's file location, which would be why you're getting the error that the file is already included.

The solution would be, in your child theme's functions.php file, to change how you're accessing the "template directory" (meaning the theme folder). In the child function.php file, when you require the new version of the template-tags.php file, try some variation of include( get_stylesheet_directory() . '/includes/template-tags.php'); instead (assuming your main stylesheet is in the root of the child theme folder).

What I'm hoping to achieve is to get it to call to the child theme folder instead of the parent theme folder.

If that fails, in your child functions.php try changing the "require_once" to get_template_part('includes/template-tags.php');. Please take note in all my examples that your file path may or may not have "includes" in it and could be some other sub-folder.
It tried:

PHP:
include( get_stylesheet_directory() . '/inc/template-tags.php');
PHP:
include( get_template_directory() . '/inc/template-tags.php');
Error message: "Cannot redeclare...." (basicly what I had before).

PHP:
get_template_part('inc/template-tags.php');
No error, but no change either.

For what it's worth, in the parent functions.php the template-tags.php file is called with:
Code:
require get_template_directory() . '/inc/template-tags.php';
 
I understand now (I think).

The problem is that you are trying to modify a fuction and that fuction is already declared on the origianal template-tags.php. So when you "require" it properly with get_stylesheet_directory() it says that the fuction is already declared.

It's tricky. 2 options

Without modifying parent:
- Remove the fuction and declare again like you want (I don't know if this is possible)

Modifyng:
In the original functions.php change

require get_template_directory() . '/inc/template-tags.php'; for get_stylesheet_directory() . '/inc/template-tags.php'
 
I understand now (I think).

The problem is that you are trying to modify a fuction and that fuction is already declared on the origianal template-tags.php. So when you "require" it properly with get_stylesheet_directory() it says that the fuction is already declared.

It's tricky. 2 options

Without modifying parent:
- Remove the fuction and declare again like you want (I don't know if this is possible)

Modifyng:
In the original functions.php change

require get_template_directory() . '/inc/template-tags.php'; for get_stylesheet_directory() . '/inc/template-tags.php'
Thanks for your reply. I think you are correct.

For now, I'll just add the code snippet to the parent and save it elswhere. When I update the theme, I also have to replace the snippet. Not ideal, but also no disaster either.

Can you maybe do a temporary simple change of h1 color in the styles file of the child theme, just to ensure you have it set up properly?

When I make changes in my child functions.php and style.css they are shown on the website. So I think it's set up properly.
 
Back