How to add images to a wordpress theme when working on MAMP?
Asked Answered
H

8

18

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.

Hauptmann answered 17/1, 2012 at 1:26 Comment(0)
S
28

This works for me:

<img src="<?php echo get_bloginfo('template_url') ?>/images/logo.png"/>

See get bloginfo() function for more info.

Scarlett answered 5/9, 2012 at 11:38 Comment(0)
F
11

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">
Formulary answered 2/6, 2017 at 5:48 Comment(0)
S
7
<?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">
Strangulate answered 7/12, 2017 at 16:46 Comment(0)
R
3

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

Rosemaryrosemond answered 18/3, 2015 at 1:14 Comment(0)
E
2

You might need to do the full <img src="<?php echo $theme; ?>/images/logo.png"/>

Update I didn't read your question closely enough. If you're using the media library for the image, you'll need to specify the actual path to the image. You can figure this out from within the media library, but it's probably site_url/wp-content/uploads/2012/01/filename.jpg

In short, if you uploaded it in media, it wouldn't actually be in your theme.

Esophagitis answered 17/1, 2012 at 1:50 Comment(0)
O
2

<?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

Organza answered 21/4, 2017 at 16:29 Comment(0)
E
0

Remove the = after <?

<img src="<? $theme ?>/images/logo.png"/>

in fact, I'd probably do something like this:

<img src= <?php $theme ?> . "/images/logo.png"/>

Elsi answered 17/1, 2012 at 1:31 Comment(5)
<?= 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?Elsi
I guess not, how do I define it?Hauptmann
You'll have to post more of your code in order to answer that question.Elsi
<?= 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).Gerous
<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.Gerous
L
0

on 2021...

Just name the image as screenshot.png [ recommended image size (currently) is 1200px wide by 900px ] and put it on theme's top-level directory (where the style.css file is )

and that's all you need to do.

Legibility answered 29/12, 2020 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.