With the abundance of news pieces and the number of blog sites continuously growing, keeping track of what’s coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.
When implemented well, this automation solution saves readers time greatly. Really Simple Syndication monitors the websites that you’re interested in and synchronises all their new content updates as they come out. It takes the daunting search routine on its part, leaving you the time just to glimpse and enjoy the latest.
You can use these feeds in a number of ways. For very basic implementation, WordPress has its built-in default RSS feeds. But there are also more specialized purposes for delivering a more specific type of content, for which you may need to create a custom RSS. In this article, we’ll show you how to do this.
Please note that this tutorial is not targeted for beginner level of WordPress users.
As always, before making any major changes to a live website, remember to complete your WordPress website backup.
The first thing you need to do is initialize your custom RSS action in your theme’s functions.php file.
1
2
3
4
|
add_action( 'init', 'myRSS' );
function myRSS() {
add_feed( 'feedname', 'myRSSFeed' );
}
|
The function above adds the feed to the template through the add_feed function. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed URL http://ift.tt/1eAs9CP, and the callback function will be called to actually create the feed. So, we need to create the myRSSFeed function in theme’s functions.php file.
You can place the RSS code directly in the callback function. By using get_template_path, we can keep the functionality separate to the layout.
1
2
3
|
function myRSSFeed() {
get_template_path( 'slug', 'name' );
}
|
The get_template_path function takes the slug and name arguments to a template file like wp-content/themes/themename/slug-name.php or wp-content/themes/themename/slug.php.
Take a look on the template file.
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
|
<?php
/**
* RSS Feed Template by Julia N.
*/
$limitCount = 7; // The posts limit to show
$posts = query_posts('showposts=' . $limitCount);
// Setting up content type and charset headers
header('Content-Type: '.feed_content_type('rss-http').';charset='.get_option('blog_charset'), true);
// Setting up valid XML encoding
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<!-- Declaring XML Validators namespaces -->
<rss version="2.0"
xmlns:content="http://ift.tt/yb6HFH"
xmlns:wfw="http://ift.tt/xYRqlD"
xmlns:dc="http://ift.tt/mToXri"
xmlns:atom="http://ift.tt/r5ut6F"
xmlns:sy="http://ift.tt/wvwGq0"
xmlns:slash="http://ift.tt/y8Ql4d"
<?php do_action('rss2_ns'); ?>>
<!-- Declaring channel with articles data -->
<?php $dateTimeFormat = 'D, M d Y H:i:s'; ?>
<channel>
<title><?php bloginfo_rss('name'); ?> - Feed</title>
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<lastBuildDate><?php echo mysql2date($dateTimeFormat, get_lastpostmodified(), false); ?></lastBuildDate>
<language><?php echo get_option('rss_language'); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'daily' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php while (have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date($dateTimeFormat, get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php the_author(); ?></dc:creator>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
|
The final thing that is left to do is flush your WordPress rewrite rules. To proceed with this action easily, you need to log in to the WordPress admin panel, and go Settings -> Permalinks. Once there, just click Save Changes, this will flush the rewrite rules. Now you can access your feed at http://ift.tt/1eAs9CP.
The last thing to mention is that you can aggregate as many RSS feeds from as many sources as you’d like if you apply some ingenuity. The easiest way to do so is to make a use of WP RSS Aggregator. It is a very neat plugin for importing, merging and displaying RSS and Atom feeds on your WordPress site. This solution is very comprehensive and elegant for WordPress, so it does not take much effort and time to get started.
0 comments:
Post a Comment