Domanda

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>
È stato utile?

Soluzione

  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() )
);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top