I'm doing a post query in a tab and I want the tab to not show up if no posts are found. So far I can get custom text to show up if there are no posts, but when I try to code it to not show the tab then it doesn't show the tab even if there are posts.

function en_in_tab_content() {

// The new tab content
global $product;
$story = get_the_title();

$args = array('post_type'=>array('product'), 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC','meta_query' => array(
                'key' => 'en_anthology_stories',
                'value' => $story,
                'compare' => 'like',
        //the loop start here
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <?php if ( has_post_thumbnail() ) :{?>
                    <a href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
                    <img src="<?php the_post_thumbnail_url('medium'); ?>" style="float: left; margin: 10px 10px 0 0;" width="129" height="200"/>
                    <?php } else : { ?>
                    <img src="http://enspirepublishing.com/forbiddenfiction/wp-content/uploads/sites/2/2017/10/ForbiddenFic_CvrPlaceholderSM.png" style="float: left; margin: 10px 10px 0 0;"/>
                    <?php }
                    endif; ?>

            <b><a href="<?php echo get_permalink(); ?>"><?php echo the_title();?></a></b><br><br>
            <?php the_content();?>
       <?php endwhile; else:?>
 <style>li.en_in_tab_tab { display:none !important; }</style>

 <?php endif;


So that makes it not show up at all even if there are posts. I've also tried using

 unset ($tabs['en_in_tab']);

but no luck there either.

  • and the style tag shows up in the html even if there are posts? – inarilo Jan 6 '18 at 6:12
  • @inarilo yes, the style tag shows – Kaye O'Malley Jan 7 '18 at 14:16
  • does the function run more than once? – inarilo Jan 8 '18 at 8:13
  • @inarilo As far as I can tell, no. But I'm not sure how to check for that. – Kaye O'Malley Jan 8 '18 at 20:42
  • are there posts for multiple values of $story? – inarilo Jan 9 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.