Grow your CSS skills. Land your dream job.

Simplepie – Multiple SEPARATE feeds – HELP!!

  • # July 26, 2009 at 11:43 pm

    How do you $feed->get_items as $item in separate divs for separate blogs? Like if I have two columns set up for two different blogs how do I specify which items to get from which blog? From what I’ve read it has something to do with get_feed but I’m extremely new to PHP so I’m faceplanting bigtime on getting this to work.

    # July 27, 2009 at 12:07 am

    Nevermind. I resolved the problem, didn’t realize I could treat the $feed variable as any other variable and just use two separate feeds $feed1 $feed2…

    :D Woot!

    # October 16, 2009 at 11:32 am

    Hi

    I’m looking to achieve the same i.e. 2 separate feeds in different DIVs

    Here’s my code which isn’t working, can someone point me in the correct direction?

    <?php

    //get the simplepie library
    require_once(‘inc/simplepie.inc’);

    //grab the feed
    $feed = new SimplePie();
    $feed2 = new SimplePie();

    $feed->set_feed_url(array(
    ‘http://www.theyworkforyou.com/search/rss/?s=margo+macdonald’,
    $feed2->set_feed_url(array(
    ‘http://feeds.feedburner.com/CssTricks?format=xml’,
    ));

    //enable caching
    $feed->enable_cache(true);
    $feed2->enable_cache(true);

    //provide the caching folder
    $feed->set_cache_location(‘cache’);
    $feed2->set_cache_location(‘cache’);

    //set the amount of seconds you want to cache the feed
    $feed->set_cache_duration(1800);
    $feed2->set_cache_duration(1800);

    //init the process
    $feed->init();
    $feed2->init();

    //let simplepie handle the content type (atom, RSS…)
    $feed->handle_content_type();
    $feed2->handle_content_type();

    $start = 0;
    $length = 3;

    ?>

    And then the code for each DIV:

    <?php
    /*
    Here, we’ll loop through all of the items in the feed, and $item represents the current item in the loop.
    */
    foreach ($feed->get_items($start,$length) as $item):
    ?>

    <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
    <p><?php echo $item->get_description(); ?></p>
    <p><small>Posted on <?php echo $item->get_date(‘j F Y | g:i a’); ?></small></p>

    <?php endforeach; ?>

    <?php
    /*
    Here, we’ll loop through all of the items in the feed, and $item represents the current item in the loop.
    */
    foreach ($feed2->get_items($start,$length) as $item):
    ?>

    <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
    <p><?php echo $item->get_description(); ?></p>
    <p><small>Posted on <?php echo $item->get_date(‘j F Y | g:i a’); ?></small></p>

    <?php endforeach; ?>

    # October 16, 2009 at 3:03 pm

    It would be helpful if you could say why something isn’t working – syntax error, output is not what was expected, or something.
    That said, the following looks odd:

    Code:
    //grab the feed
    $feed = new SimplePie();
    $feed2 = new SimplePie();

    $feed->set_feed_url(array(
    ‘http://www.theyworkforyou.com/search/rss/?s=margo+macdonald’,
    $feed2->set_feed_url(array(
    ‘http://feeds.feedburner.com/CssTricks?format=xml’,
    ));

    To initialise two feeds you simply need to:

    Code:
    //grab the feed
    $feed = new SimplePie(‘http://www.theyworkforyou.com/search/rss/?s=margo+macdonald’);
    $feed2 = new SimplePie(‘http://feeds.feedburner.com/CssTricks?format=xml’);
    # October 16, 2009 at 3:46 pm

    sorry, I should have included an error message – doh!

    saying that, your code example has fixed my issue – many thanks!

    # October 16, 2009 at 4:01 pm

    No problemo!

    Dave

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".