函数文档

get_term_feed_link()

💡 云策文档标注

概述

get_term_feed_link() 函数用于获取指定分类法术语的订阅源链接,支持自定义订阅类型,并处理不同固定链接设置下的 URL 生成。

关键要点

  • 函数返回指定术语的订阅源链接,失败时返回 false
  • 参数 $term 可以是术语 ID、WP_Term 对象或对象,$taxonomy 可选,$feed 指定订阅类型(如 'rss2'、'atom'),默认为 get_default_feed()
  • 内部逻辑根据 permalink_structure 设置生成链接,并应用分类法特定的过滤器
  • 相关函数包括 get_term_link()、user_trailingslashit() 等

代码示例

echo get_term_feed_link( $tag_id, 'post_tag', 'rss2' );

注意事项

  • 函数包含多个过滤器:category_feed_link、tag_feed_link、taxonomy_feed_link,可用于自定义链接
  • 自 WordPress 3.0.0 版本引入

📄 原文内容

Retrieves the feed link for a term.

Description

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

Parameters

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

Return

string|false Link to the feed for the term specified by $term and $taxonomy.

Source

function get_term_feed_link( $term, $taxonomy = '', $feed = '' ) {
	if ( ! is_object( $term ) ) {
		$term = (int) $term;
	}

	$term = get_term( $term, $taxonomy );

	if ( empty( $term ) || is_wp_error( $term ) ) {
		return false;
	}

	$taxonomy = $term->taxonomy;

	if ( empty( $feed ) ) {
		$feed = get_default_feed();
	}

	$permalink_structure = get_option( 'permalink_structure' );

	if ( ! $permalink_structure ) {
		if ( 'category' === $taxonomy ) {
			$link = home_url( "?feed=$feed&cat=$term->term_id" );
		} elseif ( 'post_tag' === $taxonomy ) {
			$link = home_url( "?feed=$feed&tag=$term->slug" );
		} else {
			$t    = get_taxonomy( $taxonomy );
			$link = home_url( "?feed=$feed&$t->query_var=$term->slug" );
		}
	} else {
		$link = get_term_link( $term, $term->taxonomy );
		if ( get_default_feed() === $feed ) {
			$feed_link = 'feed';
		} else {
			$feed_link = "feed/$feed";
		}

		$link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
	}

	if ( 'category' === $taxonomy ) {
		/**
		 * Filters the category feed link.
		 *
		 * @since 1.5.1
		 *
		 * @param string $link The category feed link.
		 * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
		 */
		$link = apply_filters( 'category_feed_link', $link, $feed );
	} elseif ( 'post_tag' === $taxonomy ) {
		/**
		 * Filters the post tag feed link.
		 *
		 * @since 2.3.0
		 *
		 * @param string $link The tag feed link.
		 * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
		 */
		$link = apply_filters( 'tag_feed_link', $link, $feed );
	} else {
		/**
		 * Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
		 *
		 * @since 3.0.0
		 *
		 * @param string $link     The taxonomy feed link.
		 * @param string $feed     Feed type. Possible values include 'rss2', 'atom'.
		 * @param string $taxonomy The taxonomy name.
		 */
		$link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
	}

	return $link;
}

Hooks

apply_filters( ‘category_feed_link’, string $link, string $feed )

Filters the category feed link.

apply_filters( ‘tag_feed_link’, string $link, string $feed )

Filters the post tag feed link.

apply_filters( ‘taxonomy_feed_link’, string $link, string $feed, string $taxonomy )

Filters the feed link for a taxonomy other than ‘category’ or ‘post_tag’.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes