php syntax - how to concatenate properly - echo bloginfo('stylesheet_directory)
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>
Soluzione
bloginfo()
does already anecho
. It will be printed before everything else in yourecho
statement. Useget_bloginfo()
instead.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.If you are using an URL provided by a WordPress function, escape it. Always.
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