Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> อินเทอร์เน็ต

วิธีสร้าง Custom RSS Feeds ใน WordPress

อะไรดีกว่า RSS Feed? Custom RSS Feeds เหรอไง! หลายคนพยากรณ์การตายของเทคโนโลยีการแชร์นี้มาเป็นกาลแล้ว แต่มันยังคงแข็งแกร่งอยู่

สถิติแสดงว่าเว็บไซต์ชั้นนำ 2,000 จาก 10,000 เว็บไซต์ชั้นนำของโลกเผยแพร่ RSS feeds อย่างสม่ำเสมอ Feedly บริการรวม RSS feed ยอดนิยม เพิ่มฐานผู้สมัครสมาชิก 900% ในสองปี และรายได้มากกว่า 400%

คุณอาจสนใจที่จะให้ผู้เยี่ยมชมประสบการณ์ผู้ใช้ที่ปรับแต่งได้เกินกว่า RSS feed เริ่มต้นของ WordPress มาสำรวจวิธีปรับแต่ง feeds ของคุณให้เหมาะกับเว็บไซต์และผู้ชม

สำรองเว็บไซต์ของคุณ

คุณจะไปที่แบ็กเอนด์ของเว็บไซต์ ดังนั้น ให้สำรองไซต์ของคุณ หากมีอะไรเสีย คุณยังมีเว็บไซต์ เนื้อหา และการตั้งค่าอยู่ครบถ้วน อย่าละเลยขั้นตอนนี้ ไม่ว่าจะเกิดอะไรขึ้น!

นอกจากนี้ เราจะสร้างไฟล์เทมเพลตใหม่สำหรับธีม WordPress ของคุณ ดังนั้น คุณอาจต้องการสลับไปใช้/สร้าง child theme เพื่อไม่ให้ธีมเดิมเสียหาย

การสร้าง Custom RSS Feed ใน WordPress

เพื่อเริ่มต้น เราจะทำงานกับไฟล์ "functions.php" ในโฟลเดอร์ธีมของคุณ เปิดมันและวางโค้ดด้านล่าง:

/* This code initializes the custom RSS Feeds for your website*/ add_action( 'init', 'MyCustomRSS' ); function MyCustomRSS(){ add_feed( 'mycustomfeedname', 'MyCustomFeedCallback' ); } /* This code seeks the template for your RSS feed */ function MyCustomFeedCallback(){ get_template_part( 'rss', 'mycustomfeedname' ); // need to be in small case. }

สังเกตส่วนที่ขอให้คุณพิมพ์ชื่อ custom RSS feed ของคุณ รักษาชื่อนี้ให้เรียบง่าย เพื่อให้คุณจำได้ง่าย เพราะมันจะสร้าง feed URL ของเว็บไซต์คุณ เช่น "https://yoursite.com/feed/mycustomfeedname"

หมายเหตุ: ชื่อ custom RSS feed ของคุณต้องเป็นตัวพิมพ์เล็กเท่านั้น

สร้างเทมเพลต Custom RSS Feeds

เทมเพลตของ custom RSS feed คือข้อมูลที่ feed ของคุณต้องการเพื่อให้บริการ feed คิดว่ามันเป็นฟีเจอร์ HTML/CSS สำหรับ feed ของคุณ เพื่อทำเช่นนี้ คุณต้องสร้างไฟล์อีกไฟล์หนึ่งด้วยชื่อที่ไม่ซ้ำกัน ชื่อนี้ ต้องปฏิบัติตามข้อตกลง: "rss-mycustomfeedname.php"

บันทึกไฟล์นี้ในโฟลเดอร์ child theme ของคุณ ตอนนี้ เปิดไฟล์นี้และวางโค้ดต่อไปนี้:

<?php /** * Template Name: Custom RSS Template - YourCustomFeedName */ $postCount = 10; // The number of posts to show in the feed $postType = 'post'; // post type to display in the 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();

คุณสามารถแก้ไขไฟล์นี้เพื่อแสดงโพสต์ได้มากเท่าที่คุณต้องการ ใช้ตัวแปร $postCount เพื่อเปลี่ยนจำนวนโพสต์ที่จะแสดง ค่าเริ่มต้นคือแสดง "Posts" แต่คุณสามารถเปลี่ยนให้แสดงประเภทโพสต์อื่นได้โดยเปลี่ยนตัวแปร $postType ฟีเจอร์ย่อหน้าจะแสดงย่อหน้าสำหรับโพสต์ของคุณ หากโพสต์ของคุณไม่มีย่อหน้า มันจะแสดง 120 คำแรกในบทความ

เพิ่มฟังก์ชันการทำงานให้ Custom RSS Feeds

ตอนนี้คุณมีเทมเพลตที่ทำงานแล้ว คุณสามารถเพิ่มสิ่งเพิ่มเติมเข้าไปได้

แสดงรูปภาพขนาดเล็ก (Thumbnail) ใน Feeds

หากคุณต้องการรวมรูปภาพขนาดเล็กของโพสต์ใน feed ของคุณ คัดลอกและวางโค้ดต่อไปนี้ไปที่ด้านล่างของไฟล์ "functions.php" ของคุณ

หากโพสต์ของคุณมีรูปภาพขนาดเล็ก ฟังก์ชันนี้จะค้นหามันและนำเสนอร่วมกับข้อความ feed ของคุณ หากไม่มีรูปภาพ มันจะไม่ทำอะไร

/*This code adds thumbnail feature to your 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("<image>{$thumbnail}</image>"); } }

ตั้งค่าภาษา Feed

บางครั้งคุณอาจต้องประกาศภาษาสำหรับ custom feed ของคุณเพื่อให้มันทำงาน

ทำได้โดยใช้โค้ดนี้ในไฟล์ "functions.php" ของคุณ:

function myFeedLanguage(){ update_option( 'rss_language', 'en' ); } add_action( 'admin_init', 'myFeedLanguage' );

สิ่งนี้จะกำหนดค่าภาษาของคุณเป็นภาษาอังกฤษ หากคุณตั้งใจจะเปลี่ยนเป็นภาษาอื่น ให้แก้ไข en ในโค้ดฟังก์ชันเป็นภาษาที่คุณต้องการ รับรายการรหัสภาษาทั้งหมดจากลิงก์นี้

ทำให้ Custom Feed ของคุณทำงาน

เพื่อให้ custom RSS feed ของคุณทำงาน คุณต้องนำ rewrite flush ไปใช้กับระบบ WordPress ของคุณ ทั้งสิ่งที่คุณต้องทำคือไปที่ "Settings → Permalinks" และคลิก "Save Changes"

สรุป

Custom RSS feed เป็นวิธีที่ดีในการนำเสนอเนื้อหาของคุณให้กับผู้ที่อาจไม่มีการเชื่อมต่อเพียงพอที่จะเข้าถึงเว็บไซต์เต็มรูปแบบของคุณ มันยังช่วยให้ผู้ชมใหม่ค้นพบคุณผ่านแอป RSS feed บน Android และ iOS คุณควรลองทำดูอย่างแน่นอน