Hide item with php unless one item exists

I have a blog using WordPress and on the single post page it has a slider of related posts. Well it’s a cool feature but it shows the slider even if no related items match that post.

My question is can I hide the slider using php and call it if at least one item matches? Here is the code so far.

Read More

Here is my template where I display the slider

<?php get_header(); 

global $smof_data;
list($nosidebar, $content_css, $sidebar_css) = gbx_layout();
?>
<--- Template code--->

            <?php gbx_related_post($post->ID); ?>

 <--- Other template code--->
<?php get_footer(); ?>

Here is my functions I’m using to create my slider:

function gbx_related_post($post_id) {

    ?>

    <div class="recentpost-wrapper">

        <div class="image-carousel-header">

            <div class="image-carousel-title"><span>Related Posts</span></div>

            <div class="image-carousel-nav">

                <a class="prev"><i class="fa fa-chevron-left"></i></a><a class="next"><i class="fa fa-chevron-right"></i></a>

            </div>

            <div class="clear"></div>

        </div>

        <div class="iosslider recentpost-carousel <?php echo 'recentpost-carousel'.$randomid; ?>">

        <ul><?php

            $recentPosts = get_related_posts($post_id);



            global $disable_section_shortcode;

            $disable_section_shortcode_tmp = $disable_section_shortcode;

            $disable_section_shortcode = TRUE;

            while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>

            <li class="blogslider_item">

                <div>

                    <div class="blogslider_item_img">

                        <a href="<?php the_permalink(); ?>"><?php 

                            if('' != get_the_post_thumbnail())

                                $full_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));

                            else

                                $full_image = get_template_directory_uri().'/images/thumbnail_placeholder.png';



                            echo '<img class="size-full" src="' . gbx_process_image($full_image, 150, 150).'" alt="'.get_the_title(get_post_thumbnail_id($post->ID)).'"/>';

                        ?></a>

                        <?php 

                        $date = get_the_date('d');

                        $month = get_the_date('M');

                        ?>

                        <div class="blogslider_item_date"><div><b><?php echo $date; ?></b></div><div><?php echo $month; ?></div></div>

                    </div>

                    <div class="blogslider_item_desc">

                        <a class="blogslider_item_title" href="<?php the_permalink() ?>"><h3><?php the_title(); ?></h3></a>

                        <div class="blogslider_item_excerpt"><?php

                            $excerpt = get_the_content(get_the_ID());

                            $excerpt = do_shortcode($excerpt);

                            $excerpt = gbx_sections_fix($excerpt);

                            $excerpt = string_limit_words($excerpt, 15);

                            echo $excerpt.'...';

                        ?></div>

                        <div class="blogslider_item_meta"><?php echo get_comments_number( get_the_ID() ); ?> COMMENTS</div>

                    </div>

                    <div class="clear"></div>

                </div>

            </li>

            <?php endwhile; wp_reset_postdata(); 

            $disable_section_shortcode = $disable_section_shortcode_tmp;

            ?>

        </ul>

        </div>

    </div>

    <script>

    (function($){

        $(window).load(function() {

            var $recentpost_jcarousel = $('.<?php echo 'recentpost-carousel'.$randomid; ?>');

            function custom_recent_posts_UpdateSliderHeight(args) {

                var height = $('.<?php echo 'recentpost-carousel'.$randomid; ?> .blogslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);

                $recentpost_jcarousel.css({ height: height });

            }

            $recentpost_jcarousel.iosSlider({

                snapToChildren: true,

                desktopClickDrag: true,

                navPrevSelector: $recentpost_jcarousel.parent().find('a.prev'),

                navNextSelector: $recentpost_jcarousel.parent().find('a.next'),

                onSliderLoaded: custom_recent_posts_UpdateSliderHeight,

                onSlideChange: custom_recent_posts_UpdateSliderHeight,

                onSliderResize: custom_recent_posts_UpdateSliderHeight

            });

        });

    })(jQuery);

    </script>

    <?php

}

and

function get_related_posts($post_id) {

    $args = wp_parse_args($args, array(

        'showposts' => -1,

        'post__not_in' => array($post_id),

        'ignore_sticky_posts' => 0,

        'category__in' => wp_get_post_categories($post_id)

    ));



    $query = new WP_Query($args);

    return $query;

}

Related posts

Leave a Reply

2 comments

  1. There is a bug in your code. $post_id should be $post->ID. You should also reset your post data first. The only way I can think of to properly do this is to combine everything in one proper function

    Your latest function should be

    function get_related_posts() {
    wp_reset_postdata();
        global $post;
        $args = wp_parse_args($args, array(
    
            'showposts' => -1,
            'no_found_rows'             => true,
            'update_post_meta_cache'    => false,
            'update_post_term_cache'    => false,
            'ignore_sticky_posts'       => 1,
            'post__not_in'              => array($post->ID),
            'category__in' => wp_get_post_categories($post->ID)
    
        ));
    
    
    
        $query = new WP_Query($args);
    
        if ( $query->have_posts() ) :
     ?>
    
        <div class="recentpost-wrapper">
    
            <div class="image-carousel-header">
    
                <div class="image-carousel-title"><span>Related Posts</span></div>
    
                <div class="image-carousel-nav">
    
                    <a class="prev"><i class="fa fa-chevron-left"></i></a><a class="next"><i class="fa fa-chevron-right"></i></a>
    
                </div>
    
                <div class="clear"></div>
    
            </div>
    
            <div class="iosslider recentpost-carousel <?php echo 'recentpost-carousel'.$randomid; ?>">
    
            <ul><?php
    
    
                global $disable_section_shortcode;
    
                $disable_section_shortcode_tmp = $disable_section_shortcode;
    
                $disable_section_shortcode = TRUE;
    
                while ( $query->have_posts() ) : $query->the_post(); ?>
    
                <li class="blogslider_item">
    
                    <div>
    
                        <div class="blogslider_item_img">
    
                            <a href="<?php the_permalink(); ?>"><?php 
    
                                if('' != get_the_post_thumbnail())
    
                                    $full_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
    
                                else
    
                                    $full_image = get_template_directory_uri().'/images/thumbnail_placeholder.png';
    
    
    
                                echo '<img class="size-full" src="' . gbx_process_image($full_image, 150, 150).'" alt="'.get_the_title(get_post_thumbnail_id($post->ID)).'"/>';
    
                            ?></a>
    
                            <?php 
    
                            $date = get_the_date('d');
    
                            $month = get_the_date('M');
    
                            ?>
    
                            <div class="blogslider_item_date"><div><b><?php echo $date; ?></b></div><div><?php echo $month; ?></div></div>
    
                        </div>
    
                        <div class="blogslider_item_desc">
    
                            <a class="blogslider_item_title" href="<?php the_permalink() ?>"><h3><?php the_title(); ?></h3></a>
    
                            <div class="blogslider_item_excerpt"><?php
    
                                $excerpt = get_the_content(get_the_ID());
    
                                $excerpt = do_shortcode($excerpt);
    
                                $excerpt = gbx_sections_fix($excerpt);
    
                                $excerpt = string_limit_words($excerpt, 15);
    
                                echo $excerpt.'...';
    
                            ?></div>
    
                            <div class="blogslider_item_meta"><?php echo get_comments_number( get_the_ID() ); ?> COMMENTS</div>
    
                        </div>
    
                        <div class="clear"></div>
    
                    </div>
    
                </li>
    
                <?php endwhile; 
    
                $disable_section_shortcode = $disable_section_shortcode_tmp;
    
                ?>
    
            </ul>
    
            </div>
    
        </div>
    
        <script>
    
        (function($){
    
            $(window).load(function() {
    
                var $recentpost_jcarousel = $('.<?php echo 'recentpost-carousel'.$randomid; ?>');
    
                function custom_recent_posts_UpdateSliderHeight(args) {
    
                    var height = $('.<?php echo 'recentpost-carousel'.$randomid; ?> .blogslider_item:eq(' + (args.currentSlideNumber-1) + ')').outerHeight(true);
    
                    $recentpost_jcarousel.css({ height: height });
    
                }
    
                $recentpost_jcarousel.iosSlider({
    
                    snapToChildren: true,
    
                    desktopClickDrag: true,
    
                    navPrevSelector: $recentpost_jcarousel.parent().find('a.prev'),
    
                    navNextSelector: $recentpost_jcarousel.parent().find('a.next'),
    
                    onSliderLoaded: custom_recent_posts_UpdateSliderHeight,
    
                    onSlideChange: custom_recent_posts_UpdateSliderHeight,
    
                    onSliderResize: custom_recent_posts_UpdateSliderHeight
    
                });
    
            });
    
        })(jQuery);
    
        </script>
    
        <?php
    
    endif;
    wp_reset_query();
    }
    
  2. Use the following code to see if there is any content within gbx_related_post($post->ID)

    <?php if (count($smof_data['post_related_posts']) >= 1): ?>
        <?php gbx_related_post($post->ID); ?>
    <?php endif; ?>