在echo下使用标签和php代码


Using tags and php codes together under an echo

你好,我试着返回一个图像(作为if/else代码的一部分),但是我真的做不到。

下面是我的代码:

echo "<a href="<?php the_permalink() ?>" rel="bookmark" ><img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo get_post_meta( $post->ID, 'image_value', true ); ?>&amp;w=225&amp;h=246&amp;zc=1" alt="<?php the_title(); ?>" /></a>";

正如你所看到的,这显然是因为"and"。标签属性(属性我指的是'alt' 'src'等)需要一个"而php标签只适用于' ..所以我真的不知道该怎么做,呵呵…

有什么建议吗?

顺便说一下,CMS是wordpress。如果有帮助的话…

问题是Wordpress函数,如the_permalink和bloginfo也使用echo函数,所以当你试图把两者放在一起时,它不工作。他们不退还任何东西。相反,您希望使用返回字符串的函数,这样您就可以将返回值与您想要输出的HTML连接起来。

试试这个:

echo "<a href='" . get_permalink() . "' rel='bookmark'><img src='" . get_bloginfo('template_directory') . "'/timthumb.php?src=";

你可以把剩下的填上。

注意:注意其他答案。他们似乎在回答你的问题的根源,但他们忽略了wordpress功能的细微差别。

查看PHP的字符串和如何连接它们

echo '<a href="'.the_permalink().'" rel="bookmark" ><img src="'.bloginfo( 'template_directory' ).'/timthumb.php?src='.get_post_meta( $post->ID, 'image_value', true ) .'&amp;w=225&amp;h=246&amp;zc=1" alt="'.the_title().'" /></a>';