PHP echo easy problem ;)

Ok, it’s WordPress related and I know about WordPress Stack Exchange, but I’m asking here because this is mostly a PHP question.

I want my code to display something or nothing using if statement.

Read More

The problem is I’m going to have a variable and bloginfo(‘template_directory’) in-bulit function.

I wrote this:

    <?php if (!empty($instance['example'])) 

    echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; ?>   

It works fine until $instance[‘example’] is not empty, when is – it still displays the template directory link including images/example.png.

Any ideas?

I’ve tried ” . bloginfo(‘template_directory’) . ” but doesn’t seem to work.

Related posts

Leave a Reply

6 comments

  1. PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,

     <?php if (!empty($instance['example']))  {
    
    echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; } ?> 
    

    Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.

  2. You forgot to add the : after the if statement to make an if endif; block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.

    Currently it’s only checking if for the first echo command.

  3. Try this code:

    <?php if (!empty($instance['example'])) 
    
        echo "<li><a href=". $example ."><img src=".get_bloginfo('template_directory')."/images/example.png /></a></li>";
    ?>   
    
  4. Try this

    <?php if (!empty($instance['example'])) {
    
        echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
        <?php echo "/images/example.png /></a></li>"; 
    
       }
    ?>
    
  5. I’d personally use.

    <?php 
    if( !empty( $instance['example'] ) )
        echo '<li><a href="' . $example . '"><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></a></li>'; 
    ?>
    

    First we add those missing attribute quotes.
    Second we use the stylesheet path to ensure it points to the correct location for a child theme.
    Third we call get_bloginfo to get a return value for the echo statement.
    Fourth, i also added a border="0" to the image, borders aren’t usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.

  6. Same answer as others, but with better formatted code:

    <?php 
    if(!empty($instance['example']))
    {
        echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>'; 
    }
    ?> 
    

    Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.