url rewriting - How to pass username in url

I have several author page like mysiteauthorusername1mysiteauthorusername2I want to create a dynamic url likemysite

I have several author page like

mysite/author/username1
mysite/author/username2

I want to create a dynamic url like

mysite/author/current_loggedin_username

How to create this.

I have several author page like

mysite/author/username1
mysite/author/username2

I want to create a dynamic url like

mysite/author/current_loggedin_username

How to create this.

Share Improve this question edited Aug 3, 2019 at 15:18 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Aug 3, 2019 at 15:05 user172955user172955 32 bronze badges 2
  • Can you provide more details? Currently, it is not clear to me what is going on with this dynamic link. – nmr Commented Aug 3, 2019 at 15:27
  • @nmr I want to create an author page link in navigation menu bar where the logged in user will get to see all his post. The author page link is in this format mysite/author/{logged_in_username} .I want to pass the username in the url – user172955 Commented Aug 3, 2019 at 15:32
Add a comment  | 

1 Answer 1

Reset to default 0

First, you should check if the user is logged in - use is_user_logged_in().
Next step is to get ID of current user with get_current_user_id().
Next, get the URL for the author with the given ID using get_author_posts_url().
The last step is to display the button with the link.

if ( is_user_logged_in() )
{
    $uid = get_current_user_id();
    $link = get_author_posts_url( $uid );
    printf( '<div><a href="%s">My posts</a></div>', $link );
}

You can insert such a code in the header.php file or add it as a function to functions.php file and call this function in the header.php.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273074a4619869.html

相关推荐

  • url rewriting - How to pass username in url

    I have several author page like mysiteauthorusername1mysiteauthorusername2I want to create a dynamic url likemysite

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信