Theme Code Selects and Displays Certain Related Posts – How to Hide if No Posts Found


I have a paid theme that uses the code below to find related posts (based on tags) and display them nicely. The problem is that even if there is no related posts (i.e. no posts with one or more of the same tags), the heading "Related" is still shown, with nothing below it. How can I hide the entire "block" if there is nothing to show? I think I might need to move the conditional statement to the start of the code? Thank you!

<div class="zox-post-more left zoxrel zox100">
            <div class="zox-post-main-head left zoxrel zox100">
                <h4 class="zox-post-main-title">
                    <span class="zox-post-main-title" style="color: #ff0569!important;"><?php echo esc_html(get_option('zox_more_head')); ?></span>
            <?php $zox_more_posts = get_option('zox_more_posts'); if ($zox_more_posts == "1") { ?>
                <div class="zox-post-more-grid zox-div4 left zoxrel zox100">
                    <?php global $post; $zox_related_num = get_option('zox_related_num'); $pop_days = esc_html(get_option('zox_pop_days')); $popular_days_ago = "$pop_days days ago"; $recent = new WP_Query(array('posts_per_page' => $zox_related_num, 'ignore_sticky_posts'=> 1, 'post__not_in' => array( $post->ID ), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
                        <?php get_template_part( 'parts/art', 'more' ); ?>
                    <?php endwhile; wp_reset_postdata(); ?>
            <?php } else if ($zox_more_posts == "2") { ?>
                <?php zox_related_posts(); ?>
            <?php } ?>
Matt 1 month 0 Answers 8 views 0

Leave an answer