• Resolved Amit Biswas

    (@amitbiswas06)


    Dear friends,

    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 this, 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>

    I really don’t know how to fix it. Please help me anyone.

    Thanks!
    -Amit

Viewing 2 replies - 1 through 2 (of 2 total)
  • The bloginfo() function outputs data directly. In other words, it echoes or prints when you call it.

    From the bloginfo Codex page :

    This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().

    The get_bloginfo() function does not echo or print directly, but returns the data for you to manipulate or output yourself.

    It is fairly common in WordPress to have two functions that serve a similar purpose, with 1 version that echoes or prints directly, and a corresponding version that does not.

    For your echo statement, you could try something like :

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

    However, the get_stylesheet_directory_uri() function may be a more appropriate choice.

    Thread Starter Amit Biswas

    (@amitbiswas06)

    Dear Richard,

    Thank you so much for your help. I also got same help form stackoverflow.
    As I am a new coder, I was doing the php coding in a complex way. like, I was using one php block for multiple tasks. So this issue arrived.

    Now I am doing it is a smarter way and I am using multiple php blocks.

    -Amit

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘echo issue with WP function’ is closed to new replies.