0

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>
3
  • Which div? There's 4 here. Commented Mar 14, 2018 at 13:28
  • 1
    @JacobPeattie it's solved already. Thanks for showing interest.
    – mad2kx
    Commented Mar 14, 2018 at 16:29
  • Not sure why this was marked as off topic. It was a Wordpress related issue, related to checking if custom post types were empty.
    – Lisa
    Commented Jul 11, 2022 at 0:30

1 Answer 1

3

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
    Commented Mar 14, 2018 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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