Get current users post URL?

I’ve been trawling through forums but can’t find a solution to this. If anyone can help, it would be greatly appreciated!
I’m trying to display a link to current users author page (all users have author permissions).
I’m using this at the moment:

<a href="<?php echo home_url() . '/author/' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ); ?>" >My personal page</a>

The problem is, the author link displays when there is no user logged in. How can I only display the author link to users that are logged in?

Solutions Collecting From Web of "Get current users post URL?"

Well if your code is working properly and the only problem is that it displays when no one is logged in then try the code below. Because right now you are saying “Hey WP just echo out this” – so it does it. You need to change it to: “Hey WP if the user is logged in show the link to his page and if not show the login link” like that:

<?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . home_url() . '/author/' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ) . '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
?>

Now to address the problem with user names with spaces you could try this code:

<?php
    if ( is_user_logged_in() ) {
        global $current_user;
        get_currentuserinfo();
        echo '<a href="' . get_author_posts_url($current_user->ID) . '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
?>

Reference:

  • is_user_logged_in
  • get_author_posts_url
  • get_currentuserinfo

@user45381 You must use get_author_posts_url along with is_user_logged_in to fetch the authors page.

You can use the below to get this done:–

<?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . get_author_posts_url( get_current_user_id() ) . '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
?>