I am using a child theme but it does not display any images. I am reading a book which suggests the following: "There is a difference in calling images from parent themes and child themes:
for parent Theme:
<?php echo get template_directory_uri(); ?>
for child Theme:
<?php bloginfo('stylesheet_directory'); ?>
Unfortunately the book doesn't say where the relevant code should be inserted.
Any ideas please.
I am using a child theme but it does not display any images. I am reading a book which suggests the following: "There is a difference in calling images from parent themes and child themes:
for parent Theme:
<?php echo get template_directory_uri(); ?>
for child Theme:
<?php bloginfo('stylesheet_directory'); ?>
Unfortunately the book doesn't say where the relevant code should be inserted.
Any ideas please.
- 1 Hi Sebastian, welcome to WPSE. Thanks for taking the tour :) You might need to provide us with some more information to help you solve this. As it stands your question is pretty broad and might be closed. Is there anything you can add by editing your question? For example, how are you calling the images at the moment? Are you seeing any errors? – Tim Malone Commented May 10, 2016 at 21:19
2 Answers
Reset to default 4You could try the following if you have a child theme and trying to reference your images from that file location, onto your custom .php files...
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/your-custom-image.jpg" />
Just make sure in your child theme location, you really have a child folder structure of...
assets -> images
Images that are uploaded through WordPress are saved to /wp-content/uploads/
independent of your theme or child theme, and commonly accessed through helper functions (e.g. the_post_thumbnail()
) that will output HTML, including the image path(s). Therefore I'm ruling out that this is what you are trying to do.
Instead I'm assuming you are trying to load (static) assets from e.g. /wp-content/(child-)theme/assets/images/
. This can be done anywhere in your theme, e.g. in a template; let's say home.php
. I found the description on https://codex.wordpress/Function_Reference/get_stylesheet_directory_uri most helpful.
In a template, you can do the following:
Child theme syntax:
<body>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/image.png" />
</body>
In the event a child theme is being used, this function will return the child's theme directory URI. Use
get_template_directory_uri()
to avoid being overridden by a child theme.
for a parent theme, the syntax would be:
<body>
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/image.png" />
</body>
Similar references are often made when enqueuing scripts or stylesheets in functions.php
:
/*
* Enqueue a script with the correct path.
*/
function wpdocs_scripts_method() {
wp_enqueue_script(
'custom_script',
get_template_directory_uri() . '/js/custom_script.js',
array('jquery')
);
}
Last code example was contributed by bhlarsen on https://developer.wordpress/reference/functions/get_template_directory_uri/