函数文档

get_category_feed_link()

💡 云策文档标注

概述

get_category_feed_link() 函数用于获取指定分类的订阅源链接,支持自定义订阅类型如 RSS2 或 Atom。该函数基于 get_term_feed_link() 实现,适用于 WordPress 开发者处理分类内容订阅需求。

关键要点

  • 函数返回指定分类的订阅源链接,参数 $cat 可以是分类 ID 或 WP_Term 对象,$feed 可选指定订阅类型(如 'rss2'、'atom'),默认为 get_default_feed() 的值。
  • 函数内部调用 get_term_feed_link() 处理,确保与术语订阅源逻辑一致,适用于分类归档页面自动显示订阅链接。
  • 相关函数包括 feed_links_extra() 用于显示额外订阅链接,get_category_rss_link()(已弃用)用于获取 RSS2 链接,wp_xmlrpc_server::mw_getCategories() 用于 XML-RPC 分类列表。

代码示例

// 返回分类 ID 2 的默认订阅源链接
get_category_feed_link( '2', '' );

// 在分类页面自动显示 RSS 订阅链接
if ( is_category() ) {
    $category = get_category( get_query_var('cat') );
    if ( ! empty( $category ) ) {
        echo '<a href="' . esc_url( get_category_feed_link( $category->cat_ID ) ) . '" title="' . sprintf( esc_attr__( 'Subscribe to this category', 'textdomain' ), $category->name ) . '" rel="nofollow">' . __( 'Subscribe!', 'txtdomain' ) . '</a>';
    }
}

注意事项

  • 函数自 WordPress 2.5.0 引入,确保兼容性;使用 get_category_rss_link() 时注意其已弃用,建议改用本函数。
  • 在模板文件中使用时应结合 is_category() 条件判断,避免非分类页面错误输出,并注意转义和本地化处理以提高安全性。

📄 原文内容

Retrieves the feed link for a category.

Description

Returns a link to the feed for all posts in a given category. A specific feed can be requested or left blank to get the default feed.

Parameters

$catint|WP_Term|objectrequired
The ID or category object whose feed link will be retrieved.
$feedstringoptional
Feed type. Possible values include 'rss2', 'atom'.
Default is the value of get_default_feed() .

Return

string Link to the feed for the category specified by $cat.

Source

function get_category_feed_link( $cat, $feed = '' ) {
	return get_term_feed_link( $cat, 'category', $feed );
}

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Automatic display of RSS

    Display an rss link automatically when viewing a category. Insert this code on the category.php or archive.php page template.

    if ( is_category() ) {
    
        $category = get_category( get_query_var('cat') );
    
        if ( ! empty( $category ) ) {
            echo '<div class="category-feed"><a href="' . esc_url( get_category_feed_link( $category->cat_ID ) ) . '" title="' . sprintf( esc_attr__( 'Subscribe to this category', 'textdomain' ), $category->name ) . '" rel="nofollow">' . __( 'Subscribe!', 'txtdomain' ) . '</a></div>';
        }
    }