• 20/12/2018 • 5 phút đọc • 11 lượt xem

Easy Custom Feeds in WordPress

Việt Lâm Coder
Việt Lâm Coder
Senior Fullstack & System Architect • Hotline: 0944.71.4545

Now that we have seen how to setup Tumblr-style posts, it would be nice to be able to segregate the Tumblr-posts category from the main feed into its own, separate feed. This would enable readers to subscribe exclusively to the Tumblr-posts feed and maybe display it in their sidebar or something.

While we’re at it, it would also be cool to be able to provide readers with a full menu of feed choices:


  • Everything feed: includes both the main posts and the Tumblr posts

  • Articles-only feed: includes only the main articles and no Tumblr stuff

  • Tumblr-only feed: includes only the Tumblr-style posts


Let's look at an overview of the process..

Overview of this tutorial


To accomplish our goals, we’ll perform the following steps:

  1. Create two custom-Page templates

  2. Create two Pages, one for the articles and one for the Tumblr-posts

  3. Assign the custom templates to their respective Pages

  4. Make sure the feeds validate and clean up theme tags


That’s the plan, now let’s wct..

Step 1: create two custom-page templates


Our two custom Page-templates will contain all of the code needed to generate our two custom feeds. These two custom templates are almost identical. For the articles-only feed, we will exclude the tumblelog category from the loop, and for the tumblr-only feed, we will include only the tumblelog category.

Let’s do the articles-only Page template first. In your theme, create a new Page-template file called “articles.php” and insert the following code:

<?php 
/*
Template Name: Articles
*/
$numposts = 10; // number of posts in feed
$posts = query_posts('showposts='.$numposts.'&cat=-1'); // replace the number 1 with the ID of your tumblelog category
$more = 1;

header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>

<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>
>
<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?> - Article Feed</title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss("description") ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<?php the_generator( 'rss2' ); ?>
<language><?php echo get_option('rss_language'); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></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>
<comments><?php comments_link(); ?></comments>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php the_author(); ?></dc:creator>
<?php the_category_rss(); ?>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php if (get_option('rss_use_excerpt')) : ?>

<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>

<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php if ( strlen( $post->post_content ) > 0 ) : ?>

<content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>
<?php else : ?>

<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php endif; ?>
<?php endif; ?>

<wfw:commentRss><?php echo get_post_comments_feed_link(); ?></wfw:commentRss>
<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>

</item>
<?php endwhile; ?>

</channel>
</rss>


Alternately, you may download the two required page-template files here.

Once you have that code in place, edit the $numposts variable according to the number of articles that you would like to display in the articles-only feed. Then edit the $posts variable according to the ID of your tumblelog category. See the comments in the code for details.

That’s it for your articles-only feed. Now, for your tumblr-only feed, create another new Page-template file called “tumblelog.php” and insert the exact same chunk of code as before. The first change that we need to make for this second template is to remove the hyphen ( - ) from the $numposts variable:

$posts = query_posts('showposts='.$numposts.'&cat=1');

This tells WordPress to include only your tumblelog category in your tumblr-only feed. For your articles-only feed, the presence of the hyphen tells WordPress to exclude your tumblelog category. Simple, but effective.

The second (and final) change that we need to make for the tumblr-only template is to give it a new template name in the first line:

Template Name: Tumblelog

This will enable WordPress to distinguish between the two custom templates in the next step.

Step 2: create two Pages in the WP Admin


With your two custom Page-templates uploaded to your server, login to the WordPress Admin Area and create two new Pages:

  • Create a Page called “Articles” and assign it the “Articles” template

  • Create a Page called “Tumblelog” and assign it the “Tumblelog” template


At this point, your two new custom feeds are fully functional, appearing at the following URLs:

  • http://domain.tld/articles/ — articles-only feed

  • http://domain.tld/tumblelog/ — tumblr-only feed


Once you have some posts in your tumblr-only category, you will see that they are excluded from the articles-only feed and appear by themselves in the tumblr-only feed. Of course, your main posts feed, located at http://domain.tld/feed/ will contain all of your posts, enabling you to offer the following feeds to your readers:

  • Everything feed — includes both main posts and Tumblr posts

  • Articles-only feed — includes only main articles and no Tumblr stuff

  • Tumblr-only feed — includes only the Tumblr-style posts


Final step: validation and theme clean-up


Now that everything is working, you should take the time to double-check the validity of your new feeds. This will ensure that subscribers receive the correct content in the proper format. Rest assured, I have already validated the XMLproduced from the custom page-template, but double-checking is always a good idea in case there are any unintended side-effects from plugins or whatever.

One of our favorite validation services is available at: feedvalidator.org — go there and enter the URLs of your new feeds.

Here at DiW, we recently used this exact method to establish an articles-only feed for use at Planet WordPress. You can verify that it validates quite nicely.

Finally, after setting up your custom feeds, you should go into your theme and add some nice “subscribe” links for your readers. Also want to remind you that your new feeds are in fact Pages, such that you may want to remove them from any automatic Page-listings generated by the wp_list_pages or wp_page_menutemplate tags.

For example, you can easily exclude your two feed Pages as follows:

wp_list_pages('exclude=123,456');

Where “123” and “456” are the IDs of your Articles and Tumblelog Pages.

Wrapping it up


With the technique described in this article, you now have an easy, sure-fire way of creating custom feeds in WordPress. The code for the feed markup is highly versatile, and may be customized to suit virtually any custom-feed configuration.

Việt Lâm Coder

Việt Lâm Coder

Tác giả bài viết và chuyên gia quản trị website. Nếu bạn gặp khó khăn trong quá trình thực hiện hoặc có nhu cầu thiết kế website, lập trình web app Laravel, hãy liên hệ ngay với tôi qua Hotline / Zalo: 0944.71.4545.

☕ Mời Tác Giả Ly Cà Phê Ấm Lòng

Nếu bài viết này giúp ích cho công việc của bạn, hãy tiếp thêm động lực cho tác giả duy trì kho tri thức bằng một ly cà phê ấm lòng nhé!

Mã QR ủng hộ tác giả
Ngân hàng TMCP Quân Đội (MB Bank)TONG VIET LAM

Có Thể Bạn Quan Tâm

Cách Mở Tab Ẩn Danh Trên Safari MacBook Đơn Giản

Cách mở tab ẩn danh trên Safari MacBook sẽ giúp cho người dùng có thể bảo vệ quyền riêng tư và duyệt web một cách an toà...

Hướng dẫn 5 cách tắt update Windows 11 vĩnh viễn chi tiết nhất

Mặc định Windows 11 sẽ được tự động cập nhật trong nền. Tuy nhiên nếu vì lý do nào đó mà bạn không muốn nhận các bản...

Hướng dẫn trỏ file hosts cho domain trên hệ điều hành Windows

– Làm cách nào để 1 website có thể hoạt động trên Share hosting, VPS, Server mà không cần cấu hình các record DNS cho do...

Kích hoạt Cloudflare Turnstile trong Contact Form 7 chống spam

Cloudflare Turnstile là giải pháp xác thực không dùng CAPTCHA giúp bảo vệ website WordPress khỏi spam một cách nhẹ nhàng...

Cách Sử Dụng DeepSeek R1 Miễn Phí Trong Visual Studio Code Với Cline?

Hãy đối mặt với thực tế: Các trợ lý lập trình AI như GitHub Copilot thì tuyệt vời, nhưng chi phí đăng ký của chúng có th...

Gmail API – Tất cả những gì bạn cần biết - 2025

Nếu bạn đang tìm cách tích hợp Gmail vào các ứng dụng của riêng mình thì Gmail API chính là thứ bạn cần! Gmail là một lự...

Hướng dẫn tạo màn hình desktop ảo bằng Task View trên Surface Windows 11

Việt Lâm Coder sẽ hướng dẫn tạo lập nhiều màn hình desktop ảo bằng Task View trên Surface Windows 11 để sắp xếp các tác...

How to Transfer PuTTY Sessions To Another Windows Machine

his is an user contributed article. PuTTY is a terminal emulator application which can act as a client for the SSH, T...

Hướng dẫn cách kiểm tra cổng đang mở (Check open port) trên linux

Với nhiều năm kinh nghiệm hỗ trợ Khách hàng của Tenten, chúng tôi nhận thấy việc thường xuyên theo dõi kiểm tra hoạt độn...

Kiểm tra tất cả các port mà Cyber Panel đang cần có được mở hay không ?

Để kiểm tra tất cả các port cùng một lúc, bạn có thể sử dụng lệnh: netstat -tulpn | grep -E ':(8090|80|443|21|40110|401...

Hướng dẫn cài đặt KeyHelp Panel

Tóm tắt về KeyHelp Bạn đã có phần cứng tốt nhất cho các dự án Internet của mình nhưng việc quản lý phức tạp, hiệu suất...

Tổng hợp danh sách Control Panel VPS free tốt nhất hiện nay

Control Panel VPS free cung cấp một lựa chọn tuyệt vời cho những người dùng muốn quản lý VPS một cách dễ dàng và hiệ...