How to add images to a wordpress theme when working on MAMP?
Solution 1
This works for me:
<img src="<?php echo get_bloginfo('template_url') ?>/images/logo.png"/>
See get bloginfo() function for more info.
Solution 2
You can use the following code to add an image. This works for me:
<img src="<?php echo get_template_directory_uri(); ?>/images/filename.png">
Solution 3
<?php echo get_template_directory_uri(); ?>
as suggested gets the PARENT theme, which is OK but if you've developed a child theme (as recommended by WordPress people) you need:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/img/Logo.png">
Solution 4
Just go to your dashboard and upload your picture in the media section, then select it you'll see the image options .. then copy it's url
<img src="your/copied/url"/>
This aslo works for me in localhost
Solution 5
<?php bloginfo('template_directory'); ?>/
use for internal root folder in images to place on theme on wordpress. The forward slash is needed for the image to show
Sophie
Updated on July 09, 2022Comments
-
Sophie almost 2 years
I am new to MAMP and I am developing a theme I designed. However I can't seem to get the images to render. I am using a clear theme I got from starkerstheme.com When adding an image directly to the code I used:
<img src="<?= $theme ?>/images/logo.png"/>
But the image is not showing up, also I tried to add it to the media library and it's still not rendering.
-
Paul Dessert over 12 years
<?=
is a short tag that isn't available on all servers. That's why I thought you might have trouble with it. Are you sure$theme
is defined? -
Sophie over 12 yearsI guess not, how do I define it?
-
Paul Dessert over 12 yearsYou'll have to post more of your code in order to answer that question.
-
Cedric almost 11 years<?= is a short tag that replaces <?php echo . As relentless has mentioned, it isn't available on all servers. It depends on your php configs (php.ini file). Thus, <?php theme ?> won't display anyting. Unless $theme is not defined, and the web server echoes warnings (which it should not in production).
-
Cedric almost 11 years<img src="<?php echo get_stylesheet_directory_uri(); ?>/img/logo.png" alt="site logo" /></a> when you are using a child theme. Use get_template_directory_uri() function instead of et_stylesheet_directory_uri() is you use the parent theme.