在回声中回声 - 它有效吗?

时间:2013-10-28 14:01:07

标签: php wordpress

自从我完成了php以来已经很长时间了,对于这个愚蠢的问题感到抱歉。

这是我当前的代码,我正在尝试将URL保存为变量,以便我可以将其插入到echo中,但它看起来似乎不起作用:

<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>

<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

我已回复$old_url并且可以看到它具有正确的值,但如何将值插入echo do_shortcode url="$old_url"

这也不起作用:

<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

3 个答案:

答案 0 :(得分:1)

你需要切换你的报价。单引号按原样打印所有内容。双引号将处理变量。此外,回声中不需要回声。

<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>    

另一种不切换引号的方法是打破声明:

<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

答案 1 :(得分:1)

变量不会替换为单引号...

<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>

答案 2 :(得分:0)

单引号不允许变量解析。
例如:

$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"

所以你必须使用双引号或使用连接运算符:.