Support

Account

Home Forums General Issues relationship field: parent if/else statement based on nested if statement

Unread

relationship field: parent if/else statement based on nested if statement

  • I’ve got this code:

    <?php 
        $classes = get_sub_field('class');
        if( $classes ):
    ?>
    
        <?php
        	foreach($classes as $post):
            setup_postdata($post);
        ?>
    
            <?php
                $now = time(); // get today's date
                $expiry_date = strtotime(get_field('class-expiry-date')); // get the expiration date
                if ($now < $expiry_date): // compare the dates and show upcoming classes only
            ?>
    	        
                <div class="class">
    
                    Class details
    
                </div>
    
    	<?php endif; ?>
    
        <?php
            endforeach;
            wp_reset_postdata();
        ?>
    
    <?php else: ?>
    
    There are no upcoming classes.
    
    <?php endif; ?>

    So I have a CPT called Classes. Via the Relationship field I’m allowing my client to hand pick classes to show on the front end. Each class has an expiry date. Within a foreach statement I set up an if statement that compares the current date with the expiry date and only shows upcoming classes. I’m trying to show a note saying “there are no upcoming classes” once all the expiry dates have passed, but it’s not working. I got it working within the foreach statement, but of course it just repeats the “no classes” note based on however many classes are selected in the back end. Is there a way for the top level if statement to check if there are any upcoming classes, and if there aren’t for it to show the “no classes” note?

    Thanks in advance!

Viewing 1 post (of 1 total)

The topic ‘relationship field: parent if/else statement based on nested if statement’ is closed to new replies.