One Mom’s Journey from Fat to Skinny to Confident

ASK RONI Q&A

A Central Mash-Up Using the WordPress Fetch RSS Feed Function

0 Comments 1394 views

A couple of weeks ago Jennifer from ShesAFitChick.com asked,

I just had a quick question for you. I love how on your Roni Noone site you have all of your feeds coming in as if it is one blog. How did you set that up? I have a few sites going on at once and I would love a central mash-up like this.

Well Jen you will be happy to hear the answer it pretty easy! It’s driven by your RSS feeds. Here’s a simple tutorial for my fellow self hosted WordPress users.

Fetching the Feed

If you weren’t aware there is actually a WordPress Function called fetch_feed that, yup, you guessed it, fetches a feed.

Here’s the basic syntax

 

<?php 
// Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('http://example.com/rss/feed/goes/here');
if (!is_wp_error( $rss ) ) :      $maxitems = $rss->get_item_quantity(5); 
    $rss_items = $rss->get_items(0, $maxitems); 
endif;
?>

For the code newbie, simply copy and paste those few lines and replace ‘http://example.com/rss/feed/goes/here’ with the URL of the feed you want to include on your blog to grab the latest 5 posts.

Displaying the Feed

Once you fetched the feed you can now write code to display it. This is accomplished with a simple loop…

<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
<?php echo $item->get_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>

Note, this code does not need to be edited at all unless you want to chance the format. It will simply display the 5 items from the feed you identified in the Fetch above.

You can place these two chunks of code anywhere in your wordpress blog (sidebar, index, page, etc.) including in the main loop, like I have here on RoniNoone.com.

Integrated in the Main Loop

A simple way to incorporate RSS elements on your main page between blog posts is to keep a count and pull single items based on that count. For example, in index.php add…

<?php $count = 0; ?>

Somewhere above your main loop which will look something like this..

<?php while (have_posts()) : the_post(); ?>

Then add…

<?php $count++; ?>

Just below the main loop.

This will create a variable called $count and increase it by 1 every time it displays a blog post on your main page.

Now all you need to do is simply check the count and display a feed using a simple if statement. Here’s a simplified version of what is in my index page…

<?php if($count==1) : ?>
<?php
$rss = fetch_feed('http://greenlitebites.com/feed/');
if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(1);
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>
<h2> <a href="<?php echo $rss_items[0]->get_permalink(); ?>"> <?php echo $rss_items[0]->get_title(); ?> </a> - <?php echo $rss_items[0]->get_date('n/j/y'); ?> </h2>
<?php elseif ($count==2) : ?>
<?php
$rss = fetch_feed('http://ronisweigh.com/feed/');
if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(1);
$rss_items = $rss->get_items(0, $maxitems);
endif;
?> <h2> <a href="<?php echo $rss_items[0]->get_permalink(); ?>"> <?php echo $rss_items[0]->get_title(); ?> </a> - <?php echo $rss_items[0]->get_date('n/j/y'); ?></h2>

<?php endif; ?>

Basically that code checks that count, if it’s 1 then it displays the title of the first RSS item from the GreenLiteBites feed as an h2 and a link. If count is 2, I display the first item in the Roni’s Weigh feed.

If you made it this far you may be asking where the loop is from the "Displaying the Feed" section. Since I only want to display 1 item there is no need to loop at all. I can simply pull the item direction from the array that is created form the Fetch function. You do this simply by using bracket syntax. For example the following code…

<?php echo $rss_items[4]->get_title(); ?>

would display the 5 title in the RSS feed. Always keep in mind counting starts at 0.

Conclusion

The basic Fetch Function is an easy way to start to customize your Wordpres blog. Include the feeds of your other sites, fellow bloggers, or favorite news sources. Almost every site has a feed nowadays. Including a feed in your side bar is a great way to take the leap into wordpress customization and coding.



Leave a comment

I’d love to hear your story or thoughts on mine.

However, to prevent the massive amounts of spam I was receiving I have turned off comments on any post older than 5 days old. If you'd like to leave me a note regarding this post or anything really try me on twitter (@RoniNoone,) my Facebook page, or even IG (@RoniNoone) I'm so sorry for the inconvenience. I never thought I'd have to do this but it's gotten way out of hand and comment management has become simply too time consuming to manage.

Discussion

There are 0 comments so far.