dsjuimtq920056 2016-11-15 16:05
浏览 46
已采纳

为什么将PHP函数赋给变量执行它?

I am using this in WordPress:

<?php if ( !is_front_page()) {

$title = the_title();

echo <<<EOT

<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf">
<span class="featured-title">$title</span>
</div>

EOT;
} ?>

However, the page title is generated by the PHP BEFORE the div. It looks like the declaration of the $title variable itself is executing the the_title() function.

Why is this?

EDIT:

Thanks for the explanation! Here is the working code now:

<?php if ( !is_front_page()) {

$title = get_the_title();
$thumbnail = get_the_post_thumbnail_url();

echo <<<EOT

<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf"     style="background-image: url('$thumbnail');">
<span class="featured-title animated fadeIn">$title</span>
</div>
EOT;

} ?>
  • 写回答

3条回答 默认 最新

  • dsjj15012 2016-11-15 16:48
    关注

    the_title() is a template function that display (echo) the title. If you want to store the title in a var, use get_the_title() instead.

    Note that most of WP template functions work like this : get_the_content() vs the_content(), get_the_date() vs the_date()...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题