Home News Style Your RSS Feeds in WordPress

Style Your RSS Feeds in WordPress

by Phill

RSS feeds are an significant part almost any WordPress blog. WordPress now includes an RSS widget which lets you display feeds from any widget place.

But from time to time, you might choose to show RSS feeds from the content area of your post. You might also wish to alter the arrangement and style of your feeds.

Here we think about how to leave and design our personal RSS feeds with PHP.

1. Get RSS Feed Things

First we need to extract things from our RSS feed and then leave them as HTML components. Fortunately, WordPress has some built-in features that enable us to perform this.

Items_per_page — Amount of RSS items to display per page. A delimiter is going to be inserted following every $items_per_page amount of components.
Max_items -Maximum number of things to display in the RSS feed.

Desc_length — The maximum duration of every item description.
Item_before,item_after — What HTML to include before and after every RSS item.
Title_before,title_after — What HTML to include before and after every RSS item name.

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
function render_rss( $url,
             $args = array( "items_per_page" => 0, "max_items" => 0,
                    "desc" => false, "desc_length" => 150,
                    "item_before" => '<div class="rss-item">', "item_after" => '</div>',
                    "title_before" => '', "title_after" => '',
                    "desc_before" => '', "desc_after" => '')) {
    extract($args);
    $str = '';
    if (!$url) return '';   
    // Get a SimplePie feed object from the specified feed source.
        $rss = fetch_feed($url);
    // Figure out how many total items there are, but limit it to 5.
    if (!$max_items)
        $max_items = $rss->get_item_quantity();
    if ($max_items <= 0) return '';
    if (!$items_per_page)
        $items_per_page = $max_items;  
    
    // Build an array of all the items, starting with element 0 (first element).
        $rss_items = $rss->get_items(0, $max_items);
    $i = 0;$itemnum = 0;
    if (!is_array($rss_items) || empty($rss_items)) return $str; // no items
    else {
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item ) :
            $str .= "{$item_before}\n";
            $str .= "<a href='{$item->get_permalink()}' title='Posted {$item->get_date('j F Y | g:i a')}'>\n";
            $str .= "{$title_before}{$item->get_title()}{$title_after}</a>\n";
            
            if ($desc) {
                $str .= $desc_before . shorten_description($item->get_description(),$desc_length) . $desc_after . "\n";
            }
            $str .= "{$item_after}\n";
            $i++;$itemnum++;
            if ($i >= $items_per_page) {
                if ($nextpage && ($itemnum < $max_items)) { $str .= "< !--nextpage-->"; $i = 0;}
                else break;
            }
       endforeach;
   }
   return $str;
}

This may change later on.
In the event the max_items debate is undefined, we set it to demonstrate each the things in the feed. In the event the items_per_page debate is undefined, we place it to incorporate all the RSS items in one page.
Line 22 — Construct an array of all of the things we would like to reveal in the RSS feed.
Line 25 — Should we don’t create any feed products, then return an empty string.
Lines 28-42 — Create the appropriate HTML for every feed thing according to our input discussions.
Line 44 — Return the created HTML series.

2. Shorten RSS Item Description

Be aware that online 34 we pass every RSS item description by means of a shorten_description function.

1
2
3
4
5
6
7
8
9
10
11
12
function shorten_description($result, $max_len) {
    $result = str_replace("\n", " ", $result); 
    $result = str_replace("\r", " ", $result); 
    if (strlen($result) > $max_len) {
        $end = strpos($result, ' ', $max_len);
        if ($end !== FALSE)
            return substr($result, 0, $end) . " [...]";
        else
            return substr($result, 0, $max_len) . " [...]";
    } else
        return $result;    
}

This helps to ensure that the product descriptions become left as a streamlined paragraph of text.
We make certain that the description is shortened on a word boundary i.e., we don’t wish to decrease off the text in the midst of a note.

3. Connect Our RSS Feed Works into a WordPress Shortcode

Eventually we might want to join our RSS feed works to some WordPress shortcode. This may allow us to input

[rss url="https://feeds.feedburner.com/shiba-inu"]

To add rss for a shortcode, we utilize the add_shortcode function.

Considering that the default values are now managed by our rss_shortcode work we could simplify our render_rss be the follows

1
2
3
4
5
6
7
8
9
10
11
12
function rss_shortcode($atts) {
    $atts = shortcode_atts(array(   "url" => '',
                    "items_per_page" => 0, "max_items" => 0,
                    "desc" => false, "desc_length" => 150,
                    "item_before" => '<div class="rss-item">', "item_after" => '</div>',
                    "title_before" => '', "title_after" => '',
                    "desc_before" => '', "desc_after" => '' ),
                    $atts);
    return render_rss($atts);
}
add_shortcode('rss', 'rss_shortcode');

Be aware that the feed URL currently gets passed as part of their atts feature array.

We’re Done!
All done! This is just another example rss feed out of HubPages

1
2
3
4
function render_rss($atts) {
    extract($atts);
...
}

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More