Forums

[resolved] Display Message when RSS feed is empty (6 posts)

  1. adowdle
    Member
    Posted 4 months ago #

    I am having a problem with the code given here for RSS feeds.

    <?php // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/rss.php');
    $rss = fetch_rss('http://example.com/rss/feed/goes/here');
    $maxitems = 5;
    $items = array_slice($rss->items, 0, $maxitems);
    ?>
    
    <ul>
    <?php if (empty($items)) echo '<li>No items</li>';
    else
    foreach ( $items as $item ) : ?>
    <li><a href='<?php echo $item['link']; ?>'
    title='<?php echo $item['title']; ?>'>
    <?php echo $item['title']; ?>
    </a></li>
    <?php endforeach; ?>
    </ul>

    The site I am working on pulls an RSS feed from a calendar site; when the feed is empty, I want to display a message instead. This displays the RSS feed properly when it points to a feed w/content, but it does not display the message when the feed is empty.

    I would welcome *any* suggestions!

  2. t31os_
    Member
    Posted 4 months ago #

    It works for me, i've just plonked that into a theme on my test install, right in the header file and pointed it to an empty category feed at my website, i received the "No Items" message as expected...

    Am i missing something?

  3. esmi
    Member
    Posted 4 months ago #

    What about using if(count($items) < 1) instead of if(empty($items))?

    Just a thought...

  4. t31os_
    Member
    Posted 4 months ago #

    Both should work...

    I can't replicate the problem though, it worked straight off.. :( ...

    I'd imagine counting is slower then checking if empty... (by milliseconds, lol) .. i'd stick with the if empty... :P

  5. adowdle
    Member
    Posted 4 months ago #

    (count($items) < 1) fixed it, thanks! :D

  6. t31os_
    Member
    Posted 4 months ago #

    How ironic, i'll eat my words... :)

    ** yum yum **

    Perhaps someone with access to the codex could update the page to use count instead..

Reply

You must log in to post.

About this Topic

Tags