Có gì tốt hơn RSS Feed? Custom RSS Feed chứ! Nhiều người dự đoán cái chết của công nghệ chia sẻ này từ xưa, nhưng nó vẫn mạnh mẽ.
Thống kê cho thấy hơn 2000 trong top 10.000 trang web lớn nhất thế giới xuất bản RSS feed thường xuyên. Feedly - dịch vụ tổng hợp RSS phổ biến - tăng 900% người đăng ký trong 2 năm và doanh thu hơn 400%.
Bạn có thể muốn trải nghiệm tùy chỉnh cho người truy cập vượt qua RSS feed mặc định WordPress. Hãy khám phá cách tùy chỉnh feed cho phù hợp trang web và đối tượng.
Sao Lưu Trang Web
Bạn sẽ vào backend trang web, hãy sao lưu. Nếu có lỗi, bạn vẫn có trang web, nội dung và cài đặt nguyên vẹn. Đừng bỏ qua bước này!
Cũng vậy, chúng ta sẽ tạo file template mới cho theme WordPress, nên bạn có thể muốn chuyển sang/tạo child theme để không làm hỏng theme hiện tại.
Tạo Custom RSS Feed Trong WordPress
Để bắt đầu, làm việc với file functions.php trong thư mục theme. Mở và dán mã sau:
/* Khởi tạo custom RSS Feeds cho trang web */ add_action( 'init', 'MyCustomRSS' ); function MyCustomRSS() { add_feed( 'mycustomfeedname', 'MyCustomFeedCallback' ); } /* Tìm template cho RSS feed */ function MyCustomFeedCallback() { get_template_part( 'rss', 'mycustomfeedname' ); // phải viết thường. }
Chú ý phần yêu cầu nhập tên custom RSS feed. Đặt tên đơn giản để dễ nhớ vì nó tạo thành URL feed. VD: https://yoursite.com/feed/mycustomfeedname.
Lưu ý: Tên custom RSS feed chỉ được viết chữ thường.
Tạo Template Custom RSS Feed
Template custom RSS feed là thông tin feed cần để phục vụ feed. Hãy coi như tính năng HTML/CSS cho feed. Để làm điều này, tạo file khác với tên duy nhất. Tên BẮT BUỘC tuân theo quy ước: rss-mycustomfeedname.php.
Lưu file trong thư mục child theme. Mở file và dán mã sau:
<?php /** * Template Name: Custom RSS Template - YourCustomFeedName */ $postCount = 10; // Số bài hiển thị trong feed $postType = 'post'; // Loại bài hiển thị trong feed query_posts( array( 'post_type' => $postType, 'showposts' => $postCount ) ); $charset = get_option( 'blog_charset' ); header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . $charset, true ); ?><?xml version="1.0" encoding="<?php echo $charset; ?>"?> <rss version="2.0" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/" <?php do_action('rss2_ns'); ?>> <channel> <title><?php bloginfo_rss( 'name' ); ?> - 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> <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> <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> <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> <?php // Reset Query wp_reset_query();
Bạn có thể sửa file để hiển thị bao nhiêu bài tùy ý. Dùng biến $postCount để thay số bài hiển thị. Mặc định hiển thị "Bài viết", nhưng đổi loại bài bằng biến $postType. Tính năng trích đoạn cũng hiển thị trích dẫn. Nếu bài không có trích dẫn, sẽ hiển thị 120 từ đầu bài.
Thêm Chức Năng Cho Custom RSS Feed
Khi đã có template hoạt động, bạn có thể thêm thứ khác.
Hiển Thị Thumbnail Bài Viết Trong Feed
Nếu muốn đưa thumbnail bài viết vào feed, copy mã sau vào cuối file functions.php.
Nếu bài có thumbnail, hàm này tìm và phục vụ cùng nội dung feed. Nếu không có ảnh, không làm gì.
/* Thêm tính năng thumbnail cho custom feed */ add_action( 'rss2_item', 'custom_thumbnail_tag' ); function custom_thumbnail_tag() { global $post; if ( has_post_thumbnail( $post->ID ) ) { $thumbnail = get_attachment_link( get_post_thumbnail_id( $post->ID ) ); echo("{$thumbnail} "); } }
Đặt Ngôn Ngữ Feed
Đôi khi cần khai báo ngôn ngữ cho custom feed để hoạt động.
Làm bằng mã này trong functions.php:
function myFeedLanguage() { update_option( 'rss_language', 'en' ); } add_action( 'admin_init', 'myFeedLanguage' );
Điều này cấu hình ngôn ngữ thành tiếng Anh. Nếu muốn đổi ngôn ngữ khác, sửa en trong mã hàm thành ngôn ngữ bạn muốn. Xem danh sách mã ngôn ngữ tại đây.
Kích Hoạt Custom Feed
Để custom RSS feed hoạt động, cần áp dụng rewrite flush cho WordPress. Chỉ cần vào Cài đặt → Đường dẫn tĩnh (Permalinks) và nhấp Lưu thay đổi.
Tổng Kết
Custom RSS feed là cách tuyệt vời phục vụ nội dung cho người không đủ kết nối truy cập toàn bộ trang web. Cũng giúp khán giả mới khám phá bạn qua app RSS feed trên Android và iOS. Bạn hoàn toàn nên thử.