50

I need to echo a lot of PHP and HTML.

I already tried the obvious, but it's not working:

<?php echo '
<?php if ( has_post_thumbnail() ) {   ?>
      <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) ));?></a>
      </div>
      <?php }  ?>

      <div class="date">
      <span class="day">
        <?php the_time('d') ?></span>
      <div class="holder">
        <span class="month">
          <?php the_time('M') ?></span>
        <span class="year">
          <?php the_time('Y') ?></span>
      </div>
    </div>
    <?php }  ?>';
?>

How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 1,803
  • 3
  • 16
  • 17

7 Answers7

61

You don't need to output php tags:

<?php 
    if ( has_post_thumbnail() ) 
    {
        echo '<div class="gridly-image"><a href="'. the_permalink() .'">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )) .'</a></div>';
    }

    echo '<div class="date">
              <span class="day">'. the_time('d') .'</span>
              <div class="holder">
                <span class="month">'. the_time('M') .'</span>
                <span class="year">'. the_time('Y') .'</span>
              </div>
          </div>';
?>
Josh
  • 12,448
  • 10
  • 74
  • 118
59

You cannot run PHP code within a string like that. It just doesn't work. As well, when you're "out" of PHP code (?>), any text outside of the PHP blocks is considered output anyway, so there's no need for the echo statement.

If you do need to do multiline output from with a chunk of PHP code, consider using a HEREDOC:

<?php

$var = 'Howdy';

echo <<<EOL
This is output
And this is a new line
blah blah blah and this following $var will actually say Howdy as well

and now the output ends
EOL;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marc B
  • 356,200
  • 43
  • 426
  • 500
24

Use Heredocs to output muli-line strings containing variables. The syntax is...

$string = <<<HEREDOC
   string stuff here
HEREDOC;

The "HEREDOC" part is like the quotes, and can be anything you want. The end tag must be the only thing on it's line i.e. no whitespace before or after, and must end in a colon. For more info check out the manual.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
noel
  • 2,257
  • 3
  • 24
  • 39
5

Use the colon notation

Another option would be to use the if with a colon (:) and an endif instead of the brackets:

<?php if ( has_post_thumbnail() ): ?>
    <div class="gridly-image">
        <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false) )); ?>
        </a>
    </div>
<?php endif; ?>

<div class="date">
    <span class="day"><?php the_time('d'); ?></span>
    <div class="holder">
        <span class="month"><?php the_time('M'); ?></span>
        <span class="year"><?php the_time('Y'); ?></span>
    </div>
</div>
hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
0

The internal set of single quotes in your code is killing the string. Whenever you hit a single quote it ends the string and continues processing. You'll want something like:

$thisstring = 'this string is long \' in needs escaped single quotes or nothing will run';
usumoio
  • 3,500
  • 6
  • 31
  • 57
0

To do that, you must remove all ' charachters in your string or use an escape character. Like:

<?php
    echo '<?php
              echo \'hello world\';
          ?>';
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Afshin
  • 4,197
  • 3
  • 25
  • 34
0

Use the show_source(); function of PHP. Check for more details in show_source. This is a better method I guess.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mridul4c
  • 8,197
  • 3
  • 19
  • 28