1

This is driving me batty. Is my syntax wrong? Why is the first "." in my img src call throwing out an error? This works if I put a hard link in the img src call, FYI.

    <a class="blog-image" href="<?php the_permalink(); ?>">
        <?php if (has_post_thumbnail() ) { 
            the_post_thumbnail('medium-size');
        } else {
            echo '<img src="' . bloginfo('stylesheet_directory'); . '/img/ogpimage.png" alt="Blog Posts Placeholder">';
        } ?>
    </a>
1

1 Answer 1

3
  1. bloginfo() does already an echo. It will be printed before everything else in your echo statement. Use get_bloginfo() instead.

  2. stylesheet_directory is one of these arguments where it is better just to use the function that is called by WordPress: get_stylesheet_directory_uri(). It is easier to understand, especially in this case where one could expect a path by looking at the name of the argument string.

  3. If you are using an URL provided by a WordPress function, escape it. Always.

  4. For better readability, I would use printf() here.

Summary

printf( 
    '<img src="%s/img/ogpimage.png" alt="Blog Posts Placeholder">',
    esc_url( get_stylesheet_directory_uri() )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.