Home > Blockchain >  How to write php to html correctly?
How to write php to html correctly?

Time:08-18

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 >
        <?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?>
              <img src="<?php echo $img_url ?>">
                </div>
                  <div >
                    <p>
                      <?php echo the_title(); ?>
                        </p>
                            </div>
                            <div >
                                <p>
                                    Fribourg
                                </p>
                            </div>
                        </a>
        </li> ';

CodePudding user response:

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.

CodePudding user response:

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 >
              <img src="' . $img_url . '">
                </div>
                  <div >
                    <p>
                      ' . the_title() . '
                        </p>
                            </div>
                            <div >
                                <p>
                                    Fribourg
                                </p>
                            </div>
                        </a>
        </li> ';
  •  Tags:  
  • php
  • Related