使用 PHP 在里面添加样式属性


add style attribute inside using php

我的php文件中有这个:

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).'" >';

我还想在这里插入一个样式属性。

这是我的尝试:

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).' style="background-image: url('.get_post_image( $post->ID, "small").');" " >';

但不能正常工作,输出是这样的:

<div class="tab_icon tab_image style=" background-image:="" url(http:="" thehue.co="" wp-content="" uploads="" 2013="" 09="" untitled-22-194x150.jpg);"="" "=""></div>

如何正确应用其中的样式属性?谢谢!

1(您在第一个echo中还有另一个echo函数。您不能将一个回声放在另一个回声中。

2(您正在回显中打开PHP标签,并且它们是事先打开的。

这是正确的方法:

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).' style="background-image: url('.get_post_image( $post->ID, "small").');" " >';

您正在尝试在 echo 语句中回显回显语句。 (我确定您引用的解析器错误试图告诉您这一点...... 这没有意义:

echo 'some string'<?php echo 'some string' ?>;

当您已经在服务器端代码的上下文中时,不需要<?php echo ... ?>构造。 只需将其删除:

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).' style="background-image: url('.get_post_image( $post->ID, "small").');" " >';

试试 ;

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).' style="background-image: url('''. get_post_image( $post->ID, 'small' ).''');" >';

试试这一行,你的语句中有一个 echo 语句和一个分号,删除两者并调整你的引号,你会被设置:

echo '<div class="tab_icon tab_'.(empty($post_format) ? 'standard' : $post_format).'" style="background-image: url(' . get_post_image( $post->ID, 'small' ) . ');" >';