Conditional display of Events+ shortcode?

I am making two lists of events -- Past and Upcoming -- per event category. I'm using [eab_archive] for the upcoming list, and [eab_expired] for the past ones, and the following shortcode template to display the list:

<?php foreach ($events as $event) { ?>
                <?php $event = $event instanceof Eab_EventModel ? $event : new Eab_EventModel($event); ?>
                <article class="eab-event <?php echo eab_call_template('get_status_class', $event); ?>" id="eab-event-<?php echo $event->get_id(); ?>">
<div class="event-listing">

                                <span class="event-listing-thumbnail"><?php echo $event->get_featured_image(); ?></span>

                <span class="event-listing-title"><?php echo $event->get_title(); ?></span>
                                <span class="event-listing-meta"><?php echo eab_call_template('get_archive_content', $event); ?></span>

                                <span class="event-listing-excerpt"><?php echo $event->get_excerpt_or_fallback(200); ?></span>
</div>
                </article>
        <?php } ?>
</section>

I would like each list to have a title of Upcoming Events or Past Events, which displays only IF there are actually events in the loop.

At present, I have to put the title into the context of my page, and it displays all the time. This is OK for the "past events" list, because there are always past events. But there are not always upcoming events for every category. So when there are no upcoming events, the shortcode just returns nothing, so I'll get something like this:

Upcoming Events

Past Events
* Event 1
* Event 2
* Event 3

I would like to either show either

(A) a graceful (custom) error message (e.g., "There are no upcoming events in this category, please see <here> for other opportunities to connect."), or...

(B) nothing at all, not even a title.

In other plugins, I've seen something like this implemented within the shortcode itself, where you can specify an argument e.g., [shortcodexxx category="Name or ID" title="Upcoming Events" display="auto"] so the title is displayed (or not) automatically, based on whether events actually exist in this particular category.

I tried using the CSS "content" property with the shortcode "class" argument, like this:

[eab_archive lookahead=yes weeks=25 category="107" class="auto-title-upcoming" template="event-list-with-thumbnails"]

Then I put this in CSS:

.auto-title-upcoming::before {
  content: "  Join us for this Upcoming Event";
  color: white;
}

section.auto-title-upcoming {

  background-color: #368a8a;
    padding: 15px !important;
  border-radius: 10px;
}

It looks great....until there is no upcoming event in that category! Then it degrades poorly, showing the text "Join us for this upcoming event" from the "content" property against the background color -- but with no event listed. :slight_frown:

It seems there must be some way I can sneak an "if" statement in here, so that this whole thing only displays IF there is an event. I could probably run something using the ExecPHP plugin, but it's not my favorite option.

Any ideas or suggestions?

Thanks!

  • Kasia Swiderska

    Hello kalico,

    Did you tried putting if statement outside foreach to check if there are events that can be processed? Like that

    <?php
    if ($events) {
    foreach ($events as $event) { 
    
    	?>
                    <?php $event = $event instanceof Eab_EventModel ? $event : new Eab_EventModel($event); ?>
                    <article class="eab-event <?php echo eab_call_template('get_status_class', $event); ?>" id="eab-event-<?php echo $event->get_id(); ?>">
    <div class="event-listing">
    
                                    <span class="event-listing-thumbnail"><?php echo $event->get_featured_image(); ?></span>
    
                    <span class="event-listing-title"><?php echo $event->get_title(); ?></span>
                                    <span class="event-listing-meta"><?php echo eab_call_template('get_archive_content', $event); ?></span>
    
                                    <span class="event-listing-excerpt"><?php echo $event->get_excerpt_or_fallback(200); ?></span>
    </div>
                    </article>
            <?php
    	}
     } else {
    
    	//Put here you code
    }
    
              ?>
    </section>

    kind regards,
    Kasia

  • kalico

    Kasia Swiderska - yes!! That's exactly what I'm after. Is there a way to check the event status (open, archive, expired)? I would like to style the results differently if the event is future than if it's past (expired).

    I realize I could do this by using different templates for different lists, but you opened my eyes to the power of this template business, and now I want....more :slight_smile: y'know.... One Template to Rule Them All. :wink:

    UPDATE: Rethinking this....I actually do have a way to style it differently (as noted in my original post...doh, I should not be doing this while I'm half-asleep). But I am still interested in knowing how to test for event status in the if/else statement for other reasons :slight_smile:

  • Rupok

    Hi kalico, hope you had a wonderful day.

    Our plugin developer can give you best idea regarding how you can check event status programmatically with PHP. I've pinged him and I believe, he will come up with a workaround soon.

    Please keep in mind, our developers work round the clock and they have to deal with lots of critical issues and other things. So it may take a little while for them to check this and provide a workaround.

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.