April 26, 2007
Before I knew plug-ins existed that allowed you to implement RSS feeds into WordPress, I found a solution for this by putting my C++ programming knowledge to use and hardcoded php into my themes. I still use it and have not resorted to using a plug-in since my solution still works great.
It first started with my first theme for goSammy, I really wanted to incorporate the stuff I was digging from digg into my sidebar. I searched the topic up on the WordPress Codex and it churned up an article on how to use the fetch RSS function already incorporated into WordPress.
The article already provided the essential code I needed to get this thing going. The RSS functions built into WordPress even utilized some other outsourced functions for automatic caching. It was time to implement this into my theme.
I started off with the HTML, I created a div which would hold this list of links and labeled the div accordingly so I could easily style it in my stylesheet later.
Now that I had all the HTML tags open for my list. I started to add in the php code as outlined from the fetch RSS article found in the WordPress Codex.
require_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://feeds.feedburner.com/gosammy/digg');
As you can see, we're using the fetch_rss function to assign our RSS feed into the $rss variable. In this case, it's the location to my digg feed I've put up on feedburner.
Here is where I started to add in my own little touches from the original code in the article. Inside this for loop, I initialized a variable I called $diggcounter. I started the counter off with 1. In the next line of my if statement, I gave the condition that once this counter became greater than 5, the loop would break, essentially ending the fetching of the RSS feeds and making sure that my list would only show the 5 latest feeds.
<li><a href="<?php echo $item['link'] ?>" title="<?php echo $item['title'] ?>"><?php echo $item['title'] ?></a></li>
<?php
$diggcounter++;
}
}
?>
The above snippet is the else statement which complements the if statement I declared earlier. The function I'm creating knows that if the counter is 6 that it should stop, but what if the counter is still anywhere between 1-5? Simple, grab the next feed available! Basically what happens here is that the current feed which is being fetched will be put in <li> list element tags. Once the fetching is done, I added a counter increment. This is done by the line $diggcounter++ , basically, this will increment the counter by 1.
This function will then go back to the start of the for loop and do everything until the $diggcounter reaches 6.
</div>
<!-- End digg feed -->
I then close up my ul and div tags, and now my function on grabbing my latest 5 dugg news is finished.
For anyone who would like to use my method of incorporating RSS Feeds into your WordPress powered blog, I've cleaned up my function which is available below for you to paste into your blog. Be sure to replace ENTER_YOUR_RSS_ADDRESS_HERE with the address of the RSS feed you want to use, and feel free to replace the limit of feeds you want to display (this can be changed by replacing the number 5 in the if statement).
require_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('ENTER_YOUR_RSS_ADDRESS_HERE');
foreach ( $rss->items as $item ) {
static $counter = 1;
if ( $counter > "5" ) { break; }
else { ?>
<li><a href="<?php echo $item['link'] ?>" title="<?php echo $item['title'] ?>"><?php echo $item['title'] ?></a></li>
<?php
$counter++;
}
}
?>
If you wish to implement more than one feed, make sure to change the names of the counters so that they are unique (as I did with my example above by naming it diggcounter). Also be sure to wrap this in the proper HTML elements as I outlined above.
This is my first post
just saying HI
I would like to see a continuation of the topic
[...] How to Implement RSS Feeds into WordPress the Hard Way :: [Tagged: howto wordpress ] [...]
Awesome! And so simple, too.
Very useful information. Thanks!!
YES! This is great! Thanks so much.
i just want to say that your information is clean, to the point and relevant… Thanks So Much for the Code… i was looking all over and will visit your site again…
Great site, I will be back. Well done
It´s realy a great site, i learn a lot here. I´m from Germany so sorry about my english. See you soon.
Useful information. Thanks you