若查询并没有帖子,如何隐藏文本



如果查询没有帖子,我想用单词reviews隐藏文本。请征求意见。

is image>在此处输入图像描述

<h1 class="section-title"><span>Reviews</span></h1>
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat'         => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post(); 
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

不需要jQuery,只需将h1标记放在If statement之后和while之前。然后,如果您的查询不是空的,它将显示标题,否则不是

像这样:

<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat'         => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) { ?>
<h1 class="section-title"><span>Reviews</span></h1>
<?php while ($second_query->have_posts() ) : $second_query->the_post(); 
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

最新更新