-3

I need to output html which contains php. For php code to output data. They are not showing up now. How to do it right?

echo '<li>
    <a href="<?php echo esc_attr(the_permalink())?>">
          <div class="speaker-img">
        <?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?>
              <img src="<?php echo $img_url ?>">
                </div>
                  <div class="speaker-name">
                    <p>
                      <?php echo the_title(); ?>
                        </p>
                            </div>
                            <div class="speaker-city">
                                <p>
                                    Fribourg
                                </p>
                            </div>
                        </a>
        </li> ';
userName
  • 903
  • 2
  • 20

3 Answers3

0

Writing <?php to the output stream with an echo or similar statement doesn't cause PHP to execute it. It just writes it to the output stream where it gets sent to the browser.

There is no right way to do that.

Restructure your code so you aren't trying to nest PHP inside a string inside PHP.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0
<li>
<a href="<?php echo esc_attr(the_permalink())?>">
      <div class="speaker-img">
    <?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?>
          <img src="<?php echo $img_url ?>">
            </div>
              <div class="speaker-name">
                <p>
                  <?php echo the_title(); ?>
                    </p>
                        </div>
                        <div class="speaker-city">
                            <p>
                                Fribourg
                            </p>
                        </div>
                    </a>
    </li>
YaMus
  • 51
  • 5
-1

You can't put PHP code inside strings. You should use string concatenation.

$img_url = get_the_post_thumbnail_url( $loop->post->ID );
echo '<li>
    <a href="<?php echo esc_attr(the_permalink())?>">
          <div class="speaker-img">
              <img src="' . $img_url . '">
                </div>
                  <div class="speaker-name">
                    <p>
                      ' . the_title() . '
                        </p>
                            </div>
                            <div class="speaker-city">
                                <p>
                                    Fribourg
                                </p>
                            </div>
                        </a>
        </li> ';
Barmar
  • 741,623
  • 53
  • 500
  • 612