Forums

[resolved] echo HTML content only working with absolute URL's (5 posts)

  1. pedrovrachas
    Member
    Posted 1 year ago #

    Hi,

    I hope someone can point me in the right direction as i've been stuck on this for the last few days and it's driving me crazy, probably an easy fix for a coder.

    Basically i have the piece of code below that should only display the HTML content if it's on the Home Page, else show nothing.

    <?php if ( is_home()) {
    echo '<div id="mainNav">
    <div id="mainleft"><a href="#" rel="toggle[latestPosts]" data-openimage="http://worldcupcountdown.net/social/wp-content/themes/iMobi/images/postsCollapse.png" data-closedimage="http://worldcupcountdown.net/social/wp-content/themes/iMobi/images/postsExpand.png"><img src="http://worldcupcountdown.net/social/wp-content/themes/iMobi/images/postsCollapse.png" border="0" /></a></div>
    </div>';
    } else {
    echo '';
    }
    ?>

    Everything works fine if i use absolute URLS for my image location (which is what i reverted to so that i can continue working) but if i try to use <?php bloginfo('template_directory'); ?>/images/postsCollapse.png as the source of the images it doesnt load any images.

    I've tried using single quotes and even \ before double quotes but to no avail.

    Any help is greatly appreciated.

    Cheers

  2. DigitaLink
    Member
    Posted 1 year ago #

    Since you are already inside <?php ?> tags, you don't need to add <?php in front of bloginfo(''); before /images/postsCollapse.png. But since your echo is using single quotes (''), you DO need to escape the string to access the function. So if you try:
    data-openimage="'.bloginfo('template_directory').'/images/postsCollapse.png"
    I think it will give you what you are looking for. PHP will directly access variables and such within double quotes, but you have to escape single quotes before you get results.

  3. pedrovrachas
    Member
    Posted 1 year ago #

    @DigitalLink, thanks for the reply.

    I tried what you suggested but it's now outputing the URL up to the theme directory instead of the image.

    http://worldcupcountdown.net/social/wp-content/themes/iMobihttp://worldcupcountdown.net/social/wp-content/themes/iMobihttp://worldcupcountdown.net/social/wp-content/themes/iMobi

    ^^ That's what i get.

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    within strings, it would be more correct to use get_bloginfo() together with the tip from @DigitaLink:

    http://codex.wordpress.org/Function_Reference/get_bloginfo

  5. pedrovrachas
    Member
    Posted 1 year ago #

    @alchymyth That seemed to do the trick, thanks for the help guys.

Topic Closed

This topic has been closed to new replies.

About this Topic