简体   繁体   English

回声一个img并在src中放入一个php语句?

[英]Echo an img and put in a php statement inside the src?

How would I format this correctly? 如何正确格式化? I know I can't open a php statement inside an existing one but I'm not sure how to correct it. 我知道我无法在现有的PHP语句中打开,但是我不确定如何更正它。

echo '<img src="<?php echo dirname( get_bloginfo('stylesheet_url') ); ?>/images/isa.jpg" alt="ISA">';

像这样:

echo '<img src="'.dirname( get_bloginfo('stylesheet_url') ).'/images/isa.jpg" alt="ISA">';

像这样做

echo '<img src="'.dirname( get_bloginfo('stylesheet_url') ).'"/images/isa.jpg" alt="ISA">';

I prefer to use PHP's printf() and sprintf() for that. 我更喜欢为此使用PHP的printf()sprintf() It's easier to visualize the HTML and we just need to fill it with %s and their corresponding build up. 可视化HTML更加容易,我们只需要用%s及其对应的构建物填充它即可。

printf(
    '<img src="%s/images/isa.jpg" alt="ISA">', // base HTML
    dirname( get_bloginfo('stylesheet_url') )  // first %s converted element
);

IT is better to use get_template_directory_uri() rather than get_bloginfo(). 使用get_template_directory_uri()而不是get_bloginfo()更好。 You can use following code: 您可以使用以下代码:

echo '<img src="'.get_template_directory_uri().'/images/isa.jpg" alt="ISA">';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM