钩子文档

author_link

💡 云策文档标注

概述

author_link 是一个 WordPress 过滤器,用于修改作者页面的 URL。它允许开发者通过 add_filter 钩子自定义链接,基于作者 ID、昵称等条件进行动态调整。

关键要点

  • 过滤器名称:author_link,用于过滤作者页面 URL。
  • 参数:$link(URL 字符串)、$author_id(作者 ID)、$author_nicename(作者昵称)。
  • 用法:通过 apply_filters 调用,开发者可以添加自定义函数来修改链接。
  • 相关函数:常与 get_author_posts_url() 结合使用,用于获取作者页面 URL。
  • 版本:自 WordPress 2.1.0 引入。

代码示例

add_filter( 'author_link', 'modify_author_link', 10, 1 );
function modify_author_link( $link ) {
    $link = 'http://example.com/';
    return $link;
}
add_filter( 'author_link', 'wpdocs_author_link', 10, 3 );
function wpdocs_author_link( $link, $author_id, $author_nicename ) {
    $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( 'custom_param', 'value', $link );
    }
    if ( in_array( 'editor', (array) $author->roles ) ) {
        $link = add_query_arg( 'role', 'editor', $link );
    }
    if ( 'john_doe' === $author_nicename ) {
        $link = home_url( '/special-author-page/' );
    }
    return $link;
}

注意事项

  • 代码应添加到主题的 functions.php 文件或自定义插件中,以确保生效。
  • 使用 add_filter 时,注意参数数量(如 10, 3 表示优先级和参数个数),以避免错误。
  • 自定义函数应返回修改后的 $link,否则可能破坏原始功能。
  • 示例代码展示了基于作者 ID、角色和昵称的条件修改,开发者可根据需求调整。

📄 原文内容

Filters the URL to the author’s page.

Parameters

$linkstring
The URL to the author’s page.
$author_idint
The author’s ID.
$author_nicenamestring
The author’s nice name.

Source

$link = apply_filters( 'author_link', $link, $author_id, $author_nicename );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example migrated from Codex:

    If you add the following to the functions.php file of your child themes, you can modify the author link for all posts or using conditional tags.

    add_filter( 'author_link', 'modify_author_link', 10, 1 ); 	 	 
    function modify_author_link( $link ) {	 	 
        $link = 'http://example.com/';
    return $link;
    }

  2. Skip to note 4 content

    In this tutorial, you will learn how to use the apply_filters function with the author_link filter to modify the author link based on multiple conditions in WordPress. This can be useful if you want to customize the author link for specific authors, roles, or other criteria.

    Add the following code to your theme’s functions.php file or your custom plugin file to hook into the author_link filter and modify the author link based on multiple conditions.

    Step 1:

    add_filter( 'author_link', 'wpdocs_author_link', 10, 3 );

    Step 2:

    function wpdocs_author_link( $link, $author_id, $author_nicename ) {
        $specific_author_ids = array( 1, 2, 3 ); // Replace with your specific author IDs
        $author = get_user_by( 'ID', $author_id );
    
        // Condition 1: Check for a specific author by ID
        if ( in_array( $author_id, $specific_author_ids ) ) {
            $link = add_query_arg( 'custom_param', 'value', $link );
        }
    
        // Condition 2: Check if the author has a specific role
        if ( in_array( 'editor', (array) $author->roles ) ) {
            $link = add_query_arg( 'role', 'editor', $link );
        }
    
        // Condition 3: Modify link for a specific author nicename
        if ( 'john_doe' === $author_nicename ) {
            $link = home_url( '/special-author-page/' );
        }
    
        // Additional customizations can be added here
    
        return $link;
    }

    You have now successfully used the apply_filters function with the author_link filter to modify the author link based on multiple conditions in WordPress. This approach allows you to customize the author links to fit your specific requirements.