簡體   English   中英

如何在php中插入html,然后在html中插入php

[英]how to insert html in php then php in html

我想在HTML中插入HTML,在HTML中插入下一個PHP。 我不知道該怎么辦。

<div class="popular-posts">
            <?php 
$popularpost = new WP_Query( array( 'posts_per_page' => 3, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();

<?php echo get_the_post_thumbnail( $post_id, 'full' ); ?>
<a href="<?php echo get_permalink(); ?>"><?php the_title() ?></a>

endwhile;
?>
</div>

嘗試這個:

    <div class="popular-posts">
    <?php 
    $popularpost = new WP_Query( array( 'posts_per_page' => 3, 'meta_key' =>   'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
    while ( $popularpost->have_posts() ) : $popularpost->the_post();

    echo get_the_post_thumbnail( $post_id, 'full' ); ?>

    <a href="<?= get_permalink(); ?>"><?= the_title() ?></a>
    <?php
    endwhile;
    ?>
    </div>

首先,文件擴展名必須在(dot)php中。

要在PHP中插入HTML,您只需要用'echo'編寫即可。 即:

<html>
<body>
<p>write in html</p>
<?php
    echo '<h1>Write in PHP</h1>';
?>
</body>
</html>

您不能在HTML文件中編寫PHP代碼。 但是您可以在HTML元素內編寫PHP代碼,文件擴展名必須在(dot)php中。 即:

<html>
<body>
<?php 
    $color = 'yellow';
?>
<h1 style="color:<?php echo $color; ?>">Example</h1>
</body>
</html>

訪問http://www.w3schools.com/,以了解有關HTML和PHP的更多信息。

您沒有關閉第一個打開的PHP標記,然后又打開了另一個PHP ...,也沒有為最后一個語句"endwhile;"打開"endwhile;"

<div class="popular-posts">
            <?php 
$popularpost = new WP_Query( array( 'posts_per_page' => 3, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
?>

<?php echo get_the_post_thumbnail( $post_id, 'full' ); ?>
<a href="<?php echo get_permalink(); ?>"><?php the_title() ?></a>

<?php
endwhile;
?>
</div>

在HTML中使用PHP的一般語法是

<html>
<head>
</head>
<body>
     <?php
        // Your PHP Code
     ?>
</body>
</html>

並在PHP中使用HTML ...

    <?php
      //Your PHP Code
    ?>
     <!-- Your HTML Tags -->
    <?php
    ?>

否則你也可以這樣嘗試...

<?php
  //Your PHP Code
echo "<ul><li>Some Text</li></ul>";
  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM