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;
} ?>
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()
...
The the_title()
function is executed, because you call it. The return value is stored in the $title
variable and it's not changed afterwards.
I don't know what the function does. Maybe it echoes something and doesn't return - then the echoed valued will be just printed out, not stored in the variable and you cannot do anything about it.
If it returns something, not echoing, then you can do this:
$heredoc = <<<EOT
<div class="featured-header col-xs-12 col-sm-12 col-md-12 col-lg-12 cf">
<span class="featured-title">%t</span>
</div>
EOT;
$heredoc = sprintf($heredoc, the_title());
echo $heredoc;
Writing $title = the_title()
actually calls the function and then assign the returned value, that is normal and is standard in all programming languages. If you want to assign the function itself, you need to assign the name of the function like this :
$title = 'the_title'
And then, at the place where you want the function to be called you just write :
$title()
Which will call the function normally.
As a rule of thumb, you can say that if something is followed by () it will be called immediatly.