对于我的所有 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
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
}
}
?>