修改作者存档页链接

2024-01-19 16:25 小编

首先要做的是,修改存档页链接,如下示例:

// 修改之前 http://qianduanblog.com/author/admin  // 修改之后 http://qianduanblog.com/author/123

在wordpress里内置了关于作者存档页链接的钩子,原始的作者存档页链接是这样获取的:

/** * Retrieve the URL to the author page for the user with the ID provided. * * @since 2.1.0 * @uses $wp_rewrite WP_Rewrite * @return string The URL to the author's page. */function get_author_posts_url($author_id, $author_nicename = '') {global $wp_rewrite; $auth_ID = (int) $author_id; $link = $wp_rewrite->get_author_permastruct();if ( empty($link) ) { $file = home_url( '/' ); $link = $file . '?author=' . $auth_ID;} else {if ( '' == $author_nicename ) { $user = get_userdata($author_id);if ( !empty($user->user_nicename) ) $author_nicename = $user->user_nicename;} $link = str_replace('%author%', $author_nicename, $link); $link = home_url( user_trailingslashit( $link ) );} $link = apply_filters('author_link', $link, $author_id, $author_nicename);return $link;}

参考源文档,生成了这样的用户id的作者存档页链接:

/** * 修改url重写后的作者存档页的链接变量 * @since yundanran-3 beta 2 * 2013年10月8日23:23:49 */add_filter( 'author_link', 'yundanran_author_link', 10, 2 );function yundanran_author_link( $link, $author_id) {global $wp_rewrite;    $author_id = (int) $author_id;    $link = $wp_rewrite->get_author_permastruct();if ( empty($link) ) {        $file = home_url( '/' );        $link = $file . '?author=' . $author_id;} else {        $link = str_replace('%author%', $author_id, $link);        $link = home_url( user_trailingslashit( $link ) );}return $link;}

修改之后,在前台输出作者存档页的链接:

get_author_posts_url(1);// =>http://qianduanblog.com/author/1

说明钩子使用的是正确的,但当我们打开这个链接的时候,出现了404。这是因为,原始的作者存档页链接的url重写规则是根据用户名来的,而现在修改为了用户id,重写规则不匹配,出现了404。


在线咨询 拨打电话

电话

13363039260

微信二维码

微信二维码