0

I found the following code online and customized it to pull the xml feeds from our online calendar software and display event information on several different WP pages.

function otpl_add_class_feed() {
    if ( is_page(48) ) {
        $url = "http://orion.lib.mi.us/evanced/lib/eventsxml.asp?ag=&et=Computer+Class&nd=31&dm=exml&alltime=1";
        $xml = simplexml_load_file($url);
        foreach($xml->item as$item)
            {
                echo "<div id='computer-classes'>";
                echo "<p class='event-title'><a href='".$item->link."'>".$item->title."</a></p>";
                echo "<p class='event-date'>".$item->date." from ".$item->time." to ".$item->endtime."</p>";
                echo "<p class='event-desc'>".$item->description."</p>";
                echo "<p class='event-signup'><a href='".$item->link."'>Register Online</a>.</p>";
                echo "</div>";
            }
    } // END IF
}

How can I include a conditional statement so that if $item->date. is populated the above code will run but if $item->date. is empty then another code stating that there are no events scheduled will run? I'm sure it's something simple but my understanding of syntax isn't very strong. My few attempts have broken my functions page.

5
  • 2
    This is really a PHP question. Though I would advise you to look into WP transients so that you are not constantly reloading the XML.
    – vancoder
    Commented Apr 10, 2013 at 19:29
  • 1
    You can try if( isset($item->date) && strlen($item->date)>0 ){...}
    – birgire
    Commented Apr 10, 2013 at 19:45
  • @vancoder Thanks for the tip about WP transients. I'm guessing you have some experience using that feature. Is there an expiration period you recommend as a sweet spot for saved reloading versus timely updates for changed information?
    – otplsteve
    Commented Apr 10, 2013 at 22:00
  • There's no all-purpose sweet spot. It would entirely depend on how often your feed is likely to be updated, and how much traffic you are getting. Probably anything from a minute or 2 to several hours, usually.
    – vancoder
    Commented Apr 10, 2013 at 22:04
  • Thanks @vancoder. I'll check with my network admin and she what she thinks.
    – otplsteve
    Commented Apr 10, 2013 at 23:20

1 Answer 1

0

As @birgie pointed out, add a conditional statement to check that the date has been set:

if ( isset( $item->date ) && strlen( $item->date ) > 0 ) { ... }

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.