簡體   English   中英

如何在不使用 echo 函數的情況下添加 if 語句以使用 PHP 顯示條件 HTML

[英]How can I add an if statement to display conditional HTML using PHP without using the echo function

我正在嘗試顯示我添加到 WordPress 網站的一段 HTML 代碼,但我不想使用 echo 函數,因為它會影響循環。 我不得不使用以某種方式工作的 WP_query 來獲得我想要做的事情。 我的代碼運行良好,但我不想在某些頁面上使用它。

這是我迄今為止嘗試過的

<?php 
if(!is_front_page()){
<div class="slider-container">
                    <h4 class="slider-header">
                        <span class="slider-header_text">Trending</span>
                    </h4>
                    <div class="slider">
                       <div class="carousel-container">
                          <div class="carousel-inner">
                             <div class="track">
                        <!--Beginning of responsive slider-->
                    <?php $custom_query = new WP_Query(array(
                     'posts_per_page' => 12,
                 'category_name' => 'featured-posts',
                      )); 
                  while($custom_query->have_posts()) : $custom_query->the_post(); ?>
                                 <div class="card-container">
                                       <a href="<?php the_permalink(); ?>" class="card">
                                        <div class="img">
                                             <img src="<?php the_post_thumbnail_url('thumbnail'); ?>" alt="">
                                        </div>
                                        <div class="info">
                                          <h5><?php the_title(); ?></h5>
                                        </div>
                                       </a>
                                 </div> 
                             
               <?php endwhile; ?>
              <?php wp_reset_postdata(); // reset the query ?>
                             </div>
                           </div>
                    <div class="nav">
                       <button class="prev">
                          <i class="fas fa-angle-left"></i>
                       </button>
                       <button class="next">
                           <i class="fas fa-angle-right"></i>
                      </button>
                   </div>
   </div>
  </div>
 </div>
}


?>

但這會立即引發錯誤。

我沒有檢查你的代碼是否有效。 但是提供的沒有語法錯誤的代碼是:

<?php
if (!is_front_page()) {
    ?>
    <div class="slider-container">
        <h4 class="slider-header">
            <span class="slider-header_text">Trending</span>
        </h4>
        <div class="slider">
            <div class="carousel-container">
                <div class="carousel-inner">
                    <div class="track">
                        <!--Beginning of responsive slider-->
                        <?php $custom_query = new WP_Query(array(
                            'posts_per_page' => 12,
                            'category_name' => 'featured-posts',
                        ));
                        while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
                            <div class="card-container">
                                <a href="<?php the_permalink(); ?>" class="card">
                                    <div class="img">
                                        <img src="<?php the_post_thumbnail_url('thumbnail'); ?>" alt="">
                                    </div>
                                    <div class="info">
                                        <h5><?php the_title(); ?></h5>
                                    </div>
                                </a>
                            </div>

                        <?php endwhile; ?>
                        <?php wp_reset_postdata(); // reset the query
                        ?>
                    </div>
                </div>
                <div class="nav">
                    <button class="prev">
                        <i class="fas fa-angle-left"></i>
                    </button>
                    <button class="next">
                        <i class="fas fa-angle-right"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
<?php } ?>

暫無
暫無

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

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