函数文档

get_post_type_archive_feed_link()

💡 云策文档标注

概述

get_post_type_archive_feed_link() 函数用于获取指定文章类型的归档页面 Feed 链接。它接受文章类型和 Feed 类型作为参数,返回链接字符串或 false(如果文章类型不存在或无归档)。

关键要点

  • 参数 $post_type 为必需,指定文章类型;$feed 为可选,默认为 get_default_feed() 的值,如 'rss2' 或 'atom'。
  • 返回值:成功时返回 Feed 链接字符串,失败时返回 false(例如文章类型不存在或无归档)。
  • 函数内部逻辑:先获取文章类型归档链接,然后根据固定链接设置和文章类型重写规则构建 Feed 链接。
  • 支持过滤器:通过 post_type_archive_feed_link 钩子可过滤返回的链接。

代码示例

// 获取 'post' 文章类型的 RSS2 Feed 链接
$feed_link = get_post_type_archive_feed_link( 'post', 'rss2' );
if ( $feed_link ) {
    echo $feed_link;
} else {
    echo 'Feed 链接不可用。';
}

注意事项

  • 确保文章类型支持归档(has_archive 为 true),否则函数可能返回 false。
  • Feed 类型参数为空时,自动使用默认 Feed(通常为 'rss2',可通过 default_feed 过滤器修改)。
  • 函数处理固定链接和非固定链接情况,自动添加查询参数或路径。

📄 原文内容

Retrieves the permalink for a post type archive feed.

Parameters

$post_typestringrequired
Post type.
$feedstringoptional
Feed type. Possible values include 'rss2', 'atom'.
Default is the value of get_default_feed() .

Return

string|false The post type feed permalink. False if the post type does not exist or does not have an archive.

Source

function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
	$default_feed = get_default_feed();
	if ( empty( $feed ) ) {
		$feed = $default_feed;
	}

	$link = get_post_type_archive_link( $post_type );
	if ( ! $link ) {
		return false;
	}

	$post_type_obj = get_post_type_object( $post_type );
	if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
		$link  = trailingslashit( $link );
		$link .= 'feed/';
		if ( $feed !== $default_feed ) {
			$link .= "$feed/";
		}
	} else {
		$link = add_query_arg( 'feed', $feed, $link );
	}

	/**
	 * Filters the post type archive feed link.
	 *
	 * @since 3.1.0
	 *
	 * @param string $link The post type archive feed link.
	 * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
	 */
	return apply_filters( 'post_type_archive_feed_link', $link, $feed );
}

Hooks

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

Filters the post type archive feed link.

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes