当简码在WP中返回空时如何使用Php if语句

问题描述 投票:0回答:1

对于我的所有 AFC 字段,我使用 php if 语句,这样如果字段为空,则不会加载包含字段数据的整个部分,例如:

                <?php if( get_field('client_intro') ): ?>
                <section id="client-intro">
                    <div class="wrapper">
                        <p><?php echo get_field('client_intro'); ?></p> 
                    </div>      
                </section>
                <?php endif; ?>

我想对包含短代码数据的部分做同样的事情,但我不知道该使用什么。该短代码由显示帖子的插件使用,所以我尝试了这个(以及许多其他):

             <?php if( have_posts() ): ?>   
             <section id="client-posts">
                 <div class="wrapper">
                 <?php echo do_shortcode('[display-posts posts_per_page="10"]'); ?>
                 </div> 
             </section>
             <?php endif; ?>

没有任何作用,即使没有帖子,该部分仍然会加载。简单地说,我只想在有帖子时加载 #client-posts 部分,如果没有帖子,则不加载。

php wordpress
1个回答
0
投票

您可以将您的代码替换为以下代码

<?php
if ( shortcode_exists( 'display-posts' ) ) {
 $dposts = do_shortcode('[display-posts posts_per_page="10"]');
 if( $dposts !== ''){   
 ?>
 <section id="client-posts">
    <div class="wrapper">
    <?php echo $dposts; ?>
    </div> 
 </section>
 <?php
 }
}   
?>
© www.soinside.com 2019 - 2024. All rights reserved.