2014-12-06 63 views
-3

我正在努力尋找一種在PHP IF語句中包裝HTML和PHP的方法。用PHP將PHP和PHP包裝起來

<div class="preview"> 
        <?php while (have_posts()) : the_post(); ?> 
         <?php if (get_post_gallery()) : ?> 
          <?php echo "Heres a preview of the private gallery, please login to view more."; ?> 
          <?php echo get_post_gallery(); ?> 
         <?php endif; ?> 
        <?php endwhile; ?> 
        </div> 

<?php if (post_password_required()) { 

        the_content(); 
        } else { 

        } ?> 

所以代替the_content()的 「預覽」 代碼。

我試過這個,但它似乎沒有工作。

<?php if (post_password_required()) { 

        echo '<div class="preview">' 
         while (have_posts()) : the_post(); 
         if (get_post_gallery()) : 
          echo "Heres a preview of the private gallery, please login to view more."; 
          echo get_post_gallery(); 
         endif; 
        endwhile; 
        echo '</div>' 

        } else { 

        } ?> 
+2

這幾乎是PHP設計的目的。你有沒有試過閱讀手冊? – GordonM 2014-12-06 13:02:18

+0

好吧,你錯過了一些分號,所以你的代碼不會運行,谷歌PHP錯誤報告 – andrew 2014-12-06 13:08:13

回答

1

你嘗試過什麼?這裏有一個例子:

<?php if (post_password_required()): ?> 
    <div class="preview"> 
    <?php while (have_posts()) : the_post(); ?> 
     <?php if (get_post_gallery()) : ?> 
      <p>Heres a preview of the private gallery, please login to view more.</p> 
      <?php echo get_post_gallery(); ?> 
     <?php endif; ?> 
    <?php endwhile; ?> 
    </div> 
<?php endif; ?> 
+0

我也指出,PHP不應該包裝非動態HTML(見安德魯的答案)。 「這是預覽」文本可以將PHP標籤和「echo」去掉。 – halfer 2014-12-06 13:14:38

0
<?php if (post_password_required()) { 

        the_content(); 
        } else { //just close the php tags here ?> 

      <p> preview content goes here </p> 

        <?php //and re open them here 
        } 
        ?>