Hey folks, I've got some code that pulls several upcoming events posts and displays them on a home page. It work fine but if there are no upcoming events scheduled then there is nothing that shows up and I’d like to revise this so it displays a little ‘No events currently scheduled’ text. Any idea how I could do that? The commented out else code at the bottom sort of worked with the exception of the fact that it didn't show an upcoming event post if there was one it just showed the else. What am I missing here?
PHP:
// Display Events
function getLandingEvents() {
global $post;
$eventsArray = array();
$dateArray = array();
$counter = 0;
$postHTML = '<div class="events-list">';
$args = array('category_name' => 'events');
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();
$dateArray[] = array('date' => get_field('date', $post->ID), 'pid' => $post->ID);
$counter++;
endwhile;
endif;
foreach ($dateArray as $key => $row) {
$dateTemp[$key] = $row['date'];
$pidTemp[$key] = $row['pid'];
}
array_multisort($dateTemp, SORT_ASC, $pidTemp, SORT_ASC, $dateArray);
$today = date('Ymd');
$showCounter = 0;
for ($i = 0; $i < count($dateArray); $i++) {
$currentDay = new DateTime($today);
$eventDate = new DateTime($dateArray[$i]['date']);
$interval = $currentDay->diff($eventDate);
if (($interval->format('%R') == '+') && ($interval->format('%a') <= 60) && $showCounter < 3) {
$categories = get_the_category($dateArray[$i]['pid']);
$slug = trim($categories[0]->slug);
if (get_field('date', $dateArray[$i]['pid']) != '') {
$date = DateTime::createFromFormat('Ymd', get_field('date', $dateArray[$i]['pid']));
$mainDate = $date->format('M dS');
$month = $date->format('n');
$day = $date->format('j');
$simpleDate = $date->format('Y-m-d');
}
$postHTML .= '<div class="event">';
$postHTML .= '<a href="'.get_permalink($dateArray[$i]['pid']).'">';
$postHTML .= '<time datetime="'.$simpleDate.'">'.$month.'/'.$day.'</time>';
$postHTML .= '<div class="text-holder">';
if ($categories[0]->parent != 0) {
$childCatName = trim($categories[0]->name);
$postHTML .= '<h2>'.$childCatName.'</h2>';
}
$postHTML .= '<p><strong>'.get_the_title($dateArray[$i]['pid']).'</strong></p>';
$postHTML .= '<h3>'.get_field('event-subheading', $dateArray[$i]['pid']).'</h3>';
$postHTML .= '<div class="info">';
$postHTML .= '<p>Thursday, <time datetime="'.$simpleDate.'">'.$mainDate.', '.get_field('time', $dateArray[$i]['pid']).'</time></p>';
if (get_field('location', $dateArray[$i]['pid']) != '') {
$postHTML .= '<p>'.get_field('location', $dateArray[$i]['pid']).'</p>';
}
$postHTML .= '</div>';
$postHTML .= '</div>';
$postHTML .= '</a>';
$postHTML .= '</div>';
$showCounter++;
}
//else {
// $postHTML .= '<div class="event"><h3>Check back soon for next semester’s events!</h3></div>';
// break;
//}
}
$postHTML .= '</div>';
return $postHTML;
}