Как скрыть текст, если в запросе нет сообщений? - PullRequest
1 голос
/ 04 мая 2020

Я хочу скрыть текст со словом рецензии, если в запросе нет постов. Пожалуйста, спросите предложения.

это изображение> введите описание изображения здесь

        <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(); }?>

1 Ответ

1 голос
/ 04 мая 2020

Не нужно 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(); }?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...