-1

I am not new to Wordpress but one of my projects drives me insane. The request is to completely hide a DIV if the Custom Post Type has no content. I found several tutorials to Hide Empty Custom Fields, but this request? Is it even possible?

Below is the SourceCode to have an idea of the actual DIV and the CPT. So basically at the moment there's no post in the CPT but the DIV is displayed. But I want to make it disappear when there's no content and make it appear if there's content.

The current template is a One-Page Wordpress Website.

<div id="tour" class="block" style="margin-top: 50% !important;">
            <h3 class="block">tour > </h3>
            <div class="row events">
                <?php $query = new WP_Query( array( 'post_type' => 'tour' ) );  
                $postnum = 0;                
                if ( $query->have_posts() ) : ?>
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="col-sm-4 text-left event-cell isold">
                <h3><?php the_title(); ?></h3>
                <h4>Date: <?php echo get_post_meta($post->ID, 'eventdate', true); ?></h4>
                <h4><?php echo get_post_meta($post->ID, 'eventlocation', true); ?><br/>Starts at: <?php echo get_post_meta($post->ID, 'eventtime', true); ?></h4>
                <div>/////////////////////<br/>
                <?php echo get_post_meta($post->ID, 'eventtype', true); ?></div>
                </div>
                <?php $postnum++; // Increment counter
                if ($postnum == 3){ ?>
                <div class="clearfix hidden-xs"></div>
                <?php } ?>
                <?php endwhile;?>
                <?php else : ?>
                <?php endif; ?>
           </div>
</div>
2
  • Which div? There's 4 here. – Jacob Peattie Mar 14 '18 at 13:28
  • @JacobPeattie it's solved already. Thanks for showing interest. – mad2kx Mar 14 '18 at 16:29
2

hide div tag if Custom Post Type is empty

<?php 
$query = new WP_Query( array( 'post_type' => 'tour' ) );  
$postnum = 0;                
if ( $query->have_posts() ) : ?>
    <div id="tour" class="block" style="margin-top: 50% !important;">
        <h3 class="block">tour > </h3>
        <div class="row events">

            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="col-sm-4 text-left event-cell isold">
            <h3><?php the_title(); ?></h3>
            <h4>Date: <?php echo get_post_meta($post->ID, 'eventdate', true); ?></h4>
            <h4><?php echo get_post_meta($post->ID, 'eventlocation', true); ?><br/>Starts at: <?php echo get_post_meta($post->ID, 'eventtime', true); ?></h4>
            <div>/////////////////////<br/>
            <?php echo get_post_meta($post->ID, 'eventtype', true); ?></div>
            </div>
            <?php $postnum++; // Increment counter
            if ($postnum == 3){ ?>
            <div class="clearfix hidden-xs"></div>
            <?php } ?>
            <?php endwhile;?>
       </div>
    </div>
<?php else : ?>
<?php endif; ?>
1
  • ok I did not know it was that easy. Thanks for your help. It's working perfect. – mad2kx Mar 14 '18 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.