
How to Display RSS Feeds on your WordPress Blog
- February 5, 2016
- Leave a comment
Sometimes you might need to show external RSS feeds channel on your blog. To accomplish this, you don’t have to utilize any plugin. WordPress as of now has a built in function “fetch_feed()” that will deal with aggregating RSS feeds. In this article, you will figure out how to show external RSS feeds on blog.
Simply paste the accompanying code in any WordPress custom page that you make:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<h2><?php _e('Recent news', 'text-domain'); ?></h2> <?php /* * Get RSS Feed(s) */ include_once( ABSPATH . WPINC . '/feed.php' ); /* * Get a Simple feed object from the specified feed source. */ $rss = fetch_feed('http://www.url.com/feed/'); if (!is_wp_error($rss)) { // Checks that the object is created correctly and there is no error. /* * Figure out how many total items there are and you can limit it to how many you required as below limit it to 5. */ $maxitems = $rss->get_item_quantity(5); /* * Build an array of all the items, starting with element 0 (first element). */ $rss_items = $rss->get_items(0, $maxitems); } ?> <ul> <?php if ($maxitems == 0) { /* * If no Blog found */ ?> <li> <?php _e('No items', 'my-text-domain'); ?> </li> <?php } else { // Loop through each feed item and display each item as a hyperlink. foreach ($rss_items as $item) : ?> <li> <a href="<?php echo esc_url($item->get_permalink()); ?>" title="<?php printf(__('Posted %s', 'text-domain'), $item->get_date('j F Y | g:i a')); ?>"> <?php echo esc_html($item->get_title()); ?> </a> </li> <?php endforeach; } ?> </ul> |
Now, change the “http://www.url.com/feed/” with your Feed Resource. You can get the content of the post in loop by “$item->get_content()“.
User Comments