简体   繁体   中英

PHP Variable not displaying image that uses bloginfo('template_directory')

I need to store a hyperlinked image inside a variable, and then echo out that variable for my WordPress website. I find it more convenient to use bloginfo('template_directory') so that I can go back and forth between local and live server.

But bloginfo('template_directory') within a variable does not work, it displays nothing. But bloginfo('template_directory') will work in other areas of my website. What could be the problem?

CODE A works. Notice 127.0.0.1 in the URL

<?php
$var = '<a href="http://www.twitter.com">
    <img src="http://127.0.0.1/wp-content/themes/twentyfourteen/images/social/twitter.png" />
    </a>';
echo $var;
?>

CODE B displays nothing. Why is the below not working? Notice below I am using bloginfo('template_directory') instead of 127.0.0.1

<?php
$var = '<a href="http://www.twitter.com">
    <img src="<?php bloginfo('template_directory'); ?>/images/social/twitter.png" />
    </a>';
echo $var;
?> 

EDIT

CODE C how to replace the word "twitter" with a variable? (I am referring to line 3 below, the word "twitter" that is located in img src)

<?php
    $var = '<a href="http://www.website.com">
        <img src="' . get_bloginfo("template_directory") . '/images/social/twitter.png" />
        </a>';
    echo $var;
?> 

You can't have PHP tags in PHP tags:

Just concatenate the strings:

<?php
    $var = '<a href="http://www.twitter.com">
        <img src="' . get_bloginfo("template_directory") . '/images/social/twitter.png" />
        </a>';    //↑                                    ↑ See here
    echo $var;
?> 

You need to use get_bloginfo for you want it in a var. Also no need for php tags when in a string. http://codex.wordpress.org/Function_Reference/get_bloginfo

<?php
     $var = '<a href="http://www.twitter.com"><img src="'. get_bloginfo('template_directory') .'/images/social/twitter.png" /></a>';
     echo $var;
?>

Also also suggest using something like this instead for child themes. http://codex.wordpress.org/Function_Reference/get_template_directory_uri

<?php
    $var = '<img class="svg " src="'. get_template_directory_uri() .'/images/social/twitter.png">'
    echo $var;
?>

In response to your EDIT C code block, I'm guessing you are trying to loop through some social media icon links. This is how you might do that:

<?php
    // Array of social sites
    $sites = array(
        'twitter',
        'facebook',
        'instagram'
    );

    // Loop through each
    foreach($sites as $site){
        $var = '<img src="'. get_template_directory_uri() .'/images/social/'. $site .'.png">';
        echo $var;
    }
?>

Don't use get_bloginfo( 'template_directory' ) and for that matter get_bloginfo( 'stylesheet_directory' ) which is used for child themes. These was used in early Wordpress versions. In later versions these was "replaced" with more appropriate functions.

The correct functions to use is get_template_directory_uri() for parent themes and get_stylesheet_directory_uri() for child themes. I would suggest that you work through these pages carefully

As for your syntax, as explained in the other answer, you cannot use HTML and PHP in the same string without separating them. This is done using the string operators which you can go and read up on in the link provided

It seems that you need to add social media icons. If so, check out these two posts. Specially read the one that @toscho done to my question. That should help you quite a lot with a few issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM