Amit
Amit

Reputation: 35

echo issue with WP function

I am new WP user and please forgive me for any mistakes when explaining my issue.

I am having an issue with "echo". Here is the situation.

<?php echo '<p><img src="' . bloginfo('stylesheet_directory') . '/images/img-1.jpg" /></p>' ?>

When I am trying the above code, the output is totally different. The url is displaying outside the <p> tag.

Like this

http://127.0.0.1/wp/wp-content/themes/ThemeName<p><img src="/images/img-1.jpg" /></p>

But simple use like this - is working great. Why the above is not working?

<figure><img src="<?php bloginfo('stylesheet_directory'); ?>/images/img-1.jpg" /></figure>

Upvotes: 0

Views: 73

Answers (1)

Alexander O&#39;Mara
Alexander O&#39;Mara

Reputation: 60517

You need to use get_bloginfo to return a string which you can concatenate, bloginfo echo's automatically (in this case, while the echo string is being constructed, thus being echoed before the rest of the string).

<?php echo '<p><img src="' . get_bloginfo('stylesheet_directory') . '/images/img-1.jpg" /></p>' ?>

Alternately, you could do this:

<p><img src="<?php bloginfo('stylesheet_directory'); ?>/images/img-1.jpg" /></p>

Upvotes: 2

Related Questions