我的php变量没有打印出来。

时间:2011-07-02 21:35:09

标签: php

    <?php if ( is_user_logged_in() ) {
      echo '<a href="/community"><img id="visit-the-forums" src="<?php bloginfo('template_url') ?>/images/visit-the-forums.png" alt="Check out the Forums!" /></a>'
    } else {
      echo '<a href="/community"><img id="join-the-forums" src="<?php bloginfo('template_url') ?>/images/join-the-forums.png" alt="Join the Forums!" /></a>'
    }
    ?>

我认为我在内部设置“php bloginfo”代码的方式有些不对,但我不确定如何修复它。

2 个答案:

答案 0 :(得分:4)

以下代码应该适合您。你必须使用string concatenation

<?php if ( is_user_logged_in() ) {
  echo '<a href="/community"><img id="visit-the-forums" src="' . bloginfo('template_url'). '/images/visit-the-forums.png" alt="Check out the Forums!" /></a>'
} else {
  echo '<a href="/community"><img id="join-the-forums" src="' . bloginfo('template_url') . '/images/join-the-forums.png" alt="Join the Forums!" /></a>'
}
?>

答案 1 :(得分:3)

你有两个问题:

  1. 很可能这段代码不是 因为你是echo而执行 用单个分隔的字符串 你在里面引用它 未转义的单引号。 (你可以说是这种情况,因为即使在这个页面上,语法着色也搞砸了:)

  2. 即使你逃脱了单身 引号(例如<?php bloginfo(\'template_url\') ?>)这个 因为你正在使用而无法工作 PHP将回显PHP代码 传递给浏览器,而不是 由PHP引擎执行。

  3. 您需要做的是将bloginfo()(或get_bloginfo()的结果,请参阅下面的编辑)添加到您输出的字符串中:

      echo '<a href="/community"><img id="visit-the-forums" src="'. bloginfo('template_url') . '/images/visit-the-forums.png" alt="Check out the Forums!" /></a>'
    

    (请注意单引号作为分隔符的正确用法,以及此页面上正确的语法高亮显示:字符串偏红,代码为黑色)

    编辑:如果bloginfo这里是WordPress函数,您需要在我的代码中用get_bloginfo替换它,它实际上返回结果而不是打印它,但是你原来的问题并不清楚bloginfo是什么/做什么。