Reputation: 376
I'm trying to make a short HTML component in a PHP file to include it later in different pages of a site.
I want to use get_template_directory()
cause I thought It would return my actual theme's root URL. The theme I am using is a child theme. Instead, it seems like it is returning the parent's root directory URL.
What would be the correct way of doing all this? The piece of HTML I'm trying to include in some different pages as a component with some buttons. But I'm not including it as a plugin.
I checked everything in my child-theme's folder and everything AFAIK looks ok.
I have this code in function.php and I think it's ok from a previous question I asked here before.
<?php
add_action('wp_enqueue_scripts', 'adapt_child_enqueue_styles');
function adapt_child_enqueue_styles(){
wp_dequeue_style('theme-responsive');
wp_enqueue_style('theme-child-style', get_template_directory_uri().'/style.css');
wp_enqueue_style('theme-child-responsive', get_stylesheet_directory_uri().'/css/responsive.css');
}
?>
Upvotes: 1
Views: 4329
Reputation: 19318
I'm trying to make a short HTML component in a PHP file to include it later in different pages of a site.
What you're describing is known as a template part. While it is possible to use an include
statement coupled with get_stylesheet_directory()
there's a better way to handle it: get_template_part()
.
As another user has already pointed out; the issue you're facing at the moment is due to use of the wrong function. get_template_directory()
will return a path to the parent theme whereas get_stylesheeet_directory()
will return a path to the current/child theme.
Example using include:
include( get_stylesheet_directory() . '/my-template-part.php' );
Replacement using get_template_part()
:
get_template_part( 'my-template-part' );
The get_template_part()
function is more robust than a simple include statement. Take a look at the docs for more info: https://developer.wordpress.org/reference/functions/get_template_part/
Upvotes: 1
Reputation: 1470
Instead of using get_template_directory(), use
get_stylesheet_directory()
That will return the child theme directory rather than the parent theme directory. Read more here https://codex.wordpress.org/Function_Reference/get_stylesheet_directory
Upvotes: 4