author_feed_link
云策文档标注
概述
author_feed_link 是一个 WordPress 过滤器,用于修改指定作者的 feed 链接。它允许开发者基于作者 ID、feed 类型或作者角色等条件自定义链接。
关键要点
- 过滤器名称:author_feed_link,用于过滤作者 feed 链接。
- 参数:$link(作者 feed 链接字符串)和 $feed(feed 类型,如 'rss2' 或 'atom')。
- 用法:通过 apply_filters 调用,通常与 add_filter 结合在主题的 functions.php 或自定义插件中实现。
- 相关函数:get_author_feed_link() 用于检索作者 feed 链接。
- 版本:自 WordPress 1.5.1 引入。
代码示例
add_filter( 'author_feed_link', 'wpdocs_author_feed_link', 10, 2 );
function wpdocs_author_feed_link( $link, $feed ) {
// 条件1:检查特定作者 ID
$author_id = get_query_var( 'author' );
$specific_author_ids = array( 1, 2, 3 );
$author = get_user_by( 'ID', $author_id );
if ( in_array( $author_id, $specific_author_ids ) ) {
$link = add_query_arg( 'wpdocs_param', 'value', $link );
}
// 条件2:检查特定 feed 类型
if ( 'rss2' === $feed ) {
$link = add_query_arg( 'feed_type', 'rss2_custom', $link );
}
// 条件3:检查作者是否有特定角色
if ( in_array( 'editor', (array) $author->roles ) ) {
$link = add_query_arg( 'role', 'editor', $link );
}
return $link;
}
原文内容
Filters the feed link for a given author.
Parameters
$linkstring-
The author feed link.
$feedstring-
Feed type. Possible values include
'rss2','atom'.
Source
$link = apply_filters( 'author_feed_link', $link, $feed );
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
Skip to note 2 content
Noruzzaman
In this tutorial, you will learn how to use the apply_filters function with the author_feed_link filter to modify the author feed link based on multiple conditions in WordPress. This can be useful if you want to customize the feed link for specific authors, feed types, or other criteria.
Add the following code to your theme’s functions.php file or your custom plugin file to hook into the author_feed_link filter and modify the feed link based on multiple conditions.
add_filter( 'author_feed_link', 'wpdocs_author_feed_link', 10, 2 ); function wpdocs_author_feed_link( $link, $feed ) { // Condition 1: Check for a specific author by ID $author_id = get_query_var( 'author' ); $specific_author_ids = array( 1, 2, 3 ); // Replace with your specific author IDs $author = get_user_by( 'ID', $author_id ); if ( in_array( $author_id, $specific_author_ids ) ) { $link = add_query_arg( 'wpdocs_param', 'value', $link ); } // Condition 2: Check for a specific feed type if ( 'rss2' === $feed ) { $link = add_query_arg( 'feed_type', 'rss2_custom', $link ); } // Condition 3: Check if the author has a specific role if ( in_array( 'editor', (array) $author->roles ) ) { $link = add_query_arg( 'role', 'editor', $link ); } // Additional customizations can be added here return $link; }