Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Help with if/else statement for WordPress function

wyclef

Active Coder
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;
}
 
Hey,

Surely you don't want to break when the else it met in that case, If your removed the break then it would stay in the for loop until complete and then should show your upcoming events if i understand what your trying to do right :)
 
If i remove the break it just repeats the check back soon text 10 times. If i leave the break, it shows it once, but if I add an upcoming event, I don't see the event I just see the check back soon text.

I tried revising the first if to add this else but it didn't do anything

PHP:
if ( have_posts() ) : while ( have_posts() ) : the_post();
          $dateArray[] = array('date' => get_field('date', $post->ID), 'pid' => $post->ID);
          $counter++;
        endwhile;
        else :
    $postHTML .= '<h3>Check back soon for next semester’s events!</h3>';
  endif;
 
So, the first version of your code I think fails because you only check the first item, if that fails you break out of the for loop and get the message.

What I recommend is rather than trying to do your for loop to display the events while also doing your calculation of whether an event is in the future, instead first determine which events are in the future and store them in an array. Then, for the display you can do a simple check to see if this new array is empty. If it is, you show the no events message, otherwise you output the events you saved in the array without having to do any additional logic checks to see if they are in the future. :)
 
At the very end before your return you can do this:
Code:
if($counter === 0 || $showCounter === 0){
  $postHTML = "<p>There are no upcoming events at this time...</p>";
}
return $postHTML;
 
Back
Top Bottom