函数文档

get_search_feed_link()

💡 云策文档标注

概述

get_search_feed_link() 函数用于获取搜索结果页面的 feed 永久链接,支持自定义搜索查询和 feed 类型。它根据 WordPress 的 permalink 结构动态生成链接,并可通过 Hook 进行过滤。

关键要点

  • 函数返回搜索结果 feed 的永久链接字符串。
  • 参数 $search_query 可选,默认为空,用于指定搜索查询字符串。
  • 参数 $feed 可选,默认为 get_default_feed() 的值,可指定 feed 类型如 'rss2' 或 'atom'。
  • 函数内部处理 permalink 结构:若无 permastruct,则通过 add_query_arg 添加 feed 参数;否则,使用 trailingslashit 构建 feed 路径。
  • 提供 apply_filters('search_feed_link', $link, $feed, 'posts') Hook,允许开发者过滤生成的链接。

代码示例

function get_search_feed_link( $search_query = '', $feed = '' ) {
	global $wp_rewrite;
	$link = get_search_link( $search_query );

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

	$permastruct = $wp_rewrite->get_search_permastruct();

	if ( empty( $permastruct ) ) {
		$link = add_query_arg( 'feed', $feed, $link );
	} else {
		$link  = trailingslashit( $link );
		$link .= "feed/$feed/";
	}

	/**
	 * Filters the search feed link.
	 *
	 * @since 2.5.0
	 *
	 * @param string $link Search feed link.
	 * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
	 * @param string $type The search type. One of 'posts' or 'comments'.
	 */
	return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
}

注意事项

  • 函数自 WordPress 2.5.0 版本引入,使用时需确保版本兼容性。
  • 相关函数包括 get_search_link()、get_default_feed() 和 WP_Rewrite::get_search_permastruct(),用于辅助生成链接。
  • Hook apply_filters('search_feed_link', $link, $feed, 'posts') 可用于自定义链接输出,参数 $type 固定为 'posts'。

📄 原文内容

Retrieves the permalink for the search results feed.

Parameters

$search_querystringoptional
Search query. Default empty.
$feedstringoptional
Feed type. Possible values include 'rss2', 'atom'.
Default is the value of get_default_feed() .

Return

string The search results feed permalink.

Source

function get_search_feed_link( $search_query = '', $feed = '' ) {
	global $wp_rewrite;
	$link = get_search_link( $search_query );

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

	$permastruct = $wp_rewrite->get_search_permastruct();

	if ( empty( $permastruct ) ) {
		$link = add_query_arg( 'feed', $feed, $link );
	} else {
		$link  = trailingslashit( $link );
		$link .= "feed/$feed/";
	}

	/**
	 * Filters the search feed link.
	 *
	 * @since 2.5.0
	 *
	 * @param string $link Search feed link.
	 * @param string $feed Feed type. Possible values include 'rss2', 'atom'.
	 * @param string $type The search type. One of 'posts' or 'comments'.
	 */
	return apply_filters( 'search_feed_link', $link, $feed, 'posts' );
}

Hooks

apply_filters( ‘search_feed_link’, string $link, string $feed, string $type )

Filters the search feed link.

Changelog

Version Description
2.5.0 Introduced.