开发者

How can I get the image url in a Wordpress theme? [duplicate]

This question already has an answer here: How can I get the image url in WordPress? (1 answer) Closed 2 years ago.

I am developing a theme for wordpress. And I have many images in 'images' folder. But when I take the page in browser it is not comming.

My code is

开发者_运维百科

index.php

<ul>
<li><a href="#"><img src="images/mindset.jpg" width="145" height="32" /></a></li>

Is there any function for getting the image path in wordpress ?


src="<?php echo base_url()?>your_theme_dir/image_dir/img.ext"

As well

src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"


get_template_directory_uri();

This function will help you retrieve theme directory URI, and can be used with your images, your example below:

<img src="<?php echo get_template_directory_uri(); ?>/images/mindset.jpg" />


I had to use Stylesheet directory to work for me.

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/image.png">


You need to use

 <?php bloginfo('template_directory'); ?>

It returns the directory of the current WordPress theme.

Now for example, if your theme has some image named example.jpg inside some folder name subfolder folder in the images folder.

 themes
   |-> your theme
          |-> images
                |->subfolder
                      |-> examples.jpg

You access it like this

 <img class="article-image" src="<?php bloginfo('template_directory'); ?> /images/subfolder/example.jpg" border="0" alt="">


src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"
worked for me for wordpress 3.6 i used it to get header logo using as

<img src="<?php bloginfo('template_url'); ?>/images/logo.jpg">


If you are developing a child theme you can use:

<img src="<?php echo get_template_directory_uri(); ?>-child/images/example.png" />

get_template_directory_uri() will return url to your currently active theme (parent theme), then you add -child/, then add path to your image (the example above assumes your image is at <child-theme-directory>/images/example.png)


I strongly recommend the following:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/img-folder/your_image.jpg">

It works for almost any file you want to add to your wordpress project, be it image or CSS.


If your img folder is inside your theme folder, just follow the example below:

<img src="<?php echo  get_theme_file_uri(); ?>/img/yourimagename.jpg" class="story-img" alt="your alt text">


You asked of Function but there is an easier way too. When you will upload the image, copy it's URL which is at the top right part of the window (View Screenshot) and paste it in the src='[link you copied]'. Hope this will help if someone is looking for similar problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜