Wordpress Child Theme Calling Images

I am using a child theme but it does not display any images.I am reading a book which suggests the following: "Th

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.

Share Improve this question edited May 10, 2016 at 21:21 Sebastian asked May 10, 2016 at 21:16 SebastianSebastian 311 gold badge1 silver badge4 bronze badges 1
  • 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
Add a comment  | 

2 Answers 2

Reset to default 4

You 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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745193874a4615995.html

相关推荐

  • Wordpress Child Theme Calling Images

    I am using a child theme but it does not display any images.I am reading a book which suggests the following: "Th

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信