2

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?

3

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:

  • 1
    I would suggest adding the link to is_user_logged_in()'s Codex page to your answer, to provide the OP with a link to some relevant documentation. – Pat J Jan 19 '14 at 22:19
  • 1
    @PatJ Was going to but finally forgot writing another answer to other question about genesis. Thanks for reminder. – Borek Jan 19 '14 at 22:22
  • Rachel, Pat and Borek Thank you! must keep learning – mr_bythatmuch Jan 19 '14 at 22:27
  • If you are still there, there is one more little hiccup. If the user has a space in their username then the link doesn't include a '-' between names and so it won't properly connect to the authors URL if it has spaces? – mr_bythatmuch Jan 19 '14 at 23:22
  • 1
    You must use get_author_posts_url to get the author's page – Maruti Mohanty Jan 20 '14 at 6:30
0

@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>';
    }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.