Using WordPress methods in echo – doesn’t seem to work correctly?

Not even sure if methods is the correct terminology…

Here is the original working code:

Read More
<a href="<?php bloginfo('url'); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo.png" alt="Polished Logo" id="logo"/></a>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title"><?php bloginfo('description'); ?></p>

I wanted it to only execute on the homepage, so I wrote this:

<? 
if ( $_SERVER["REQUEST_URI"] == '/' ){
echo '<a href="'.bloginfo('url').'">
<img src="'.bloginfo('stylesheet_directory').'/images/logo.png" alt="Polished Logo" id="logo"/></a>
<img src="'.bloginfo('stylesheet_directory').'/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title">'.bloginfo('description').'</p>';
}
?>

But it outputs the bloginfo() and the other declarations completely outside the html tags I have created. For instance, with bloginfo('stylesheet_directory') it will display the directory outside the IMG tags I created.

Any ideas? Apparently my syntax isn’t correct or something….

Related posts

Leave a Reply

3 comments

  1. bloginfo function directly echoes the output. In this case you should use get_bloginfo to add the returned value to the string and echo the complete string. I believe this will work

    <?php
    if ( $_SERVER["REQUEST_URI"] == '/' ) {
      echo '<a href="'.get_bloginfo('url').'">
        <img src="'.get_bloginfo('stylesheet_directory').'/images/logo.png" alt="Polished Logo" id="logo"/></a>
        <img src="'.get_bloginfo('stylesheet_directory').'/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
        <p id="logo_title">'.get_bloginfo('description').'</p>';
    }
    ?>
    

    Here is a better alternative:

    <?php if ( $_SERVER["REQUEST_URI"] == '/' ) { ?>
    <a href="<?php bloginfo('url') ?>">
      <img src="<?php bloginfo('stylesheet_directory') ?>/images/logo.png" alt="Polished Logo" id="logo"/>
    </a>
    <img src="<?php bloginfo('stylesheet_directory') ?>/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
    <p id="logo_title"><?php bloginfo('description') ?></p>
    <?php } ?>
    

    I also suggest using the is_home() function provided by wordpress to check for the homepage instead of checking the $_SERVER['REQUEST_URI'] value.

  2. bloginfo() outputs data with echo and returns nothing, so instead of trying to concatenate everything, just output in sequence, e.g.

    echo '<a href="';
    bloginfo('url');
    echo '"><img src="';
    bloginfo('stylesheet_directory');
    //etc...
    

    Ugly I know, but see answer by Nithesh for a possible alternative.