Wordpress 如果查询没有帖子,如何隐藏文本?

Wordpress 如果查询没有帖子,如何隐藏文本?,wordpress,Wordpress,如果查询没有帖子,我想用单词reviews隐藏文本。请征求意见 是图像> 评论 不需要jQuery,只需将h1标记放在If语句之后和while之前即可。然后,如果您的查询不是空的,它将显示标题,否则不会显示 像这样: <?php $terms = get_the_terms( $post->ID , 'movies', 'string'); $term_ids = wp_list_pluc

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

是图像>

评论

不需要jQuery,只需将
h1
标记放在
If语句
之后和
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(); }?>

评论

我对h1行的错误感到困惑。没错,我忘了再次关闭和打开php标记,我更新了代码
                <?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(); }?>