0

I have this snippet:

<span><a href="<?php bloginfo('url'); ?>/writer/<?php echo get_comment_author_link(); ?>"><?php echo get_comment_author_link(); ?></a></span>

I am displaying the username of the author but it's weird because it works well on the posts... Beneath the post, the username is linked like /writer/username, which is fine, but beneath the comment, the username is linked like /writer/Username. So for example, a user is called Blackboy 3 , than beneath the post it gets /writer/blackboy-3, beneath the comment it gets /writer/Blackboy 3. This makes a problem... Because if there is a post called Blackboy 3, than the link beneath the comment gets me to the post instead of the author page.

Why is this? And how can I solve it?

1

Simple solution drop this in your functions.php

function your_get_comment_author_link () {
    global $comment;

    if ($comment->user_id == '0') {
        if (!empty ($comment->comment_author_url)) {
            $url = $comment->comment_author_url;
        } else {
            $url = '#';
        }
    } else {
        $url = get_author_posts_url($comment->user_id);
    }

    echo "<a href=\"" . $url . "\">" .get_comment_author () . "</a>";
}

If the user is registered this will link to /writer/username, if they are not it will either link to their url or to # if they haven't provided one.

Obviously to use it you'll want use it in your code using

<span><?php your_get_comment_author_link() ?></span>

Taken from Johan suggestion in comments.

  • Yes, but it should link to the author page, right? What links there? How should I make it work? – user1770896 Jul 31 '13 at 10:36
  • I just updated the post with that it will link to the /writer/username unless the user provided a different URL when they commented if you allow public comments. I believe for registered users it will always link to /writer/username. – Mark Davidson Jul 31 '13 at 10:38
  • That's incorrect. There is no anchor that will link. It just displays the author name... If I put in an anchor, it just makes it the same. Blackboy 2 instead of blackboy-2 – user1770896 Jul 31 '13 at 10:41
  • core.trac.wordpress.org/browser/tags/3.5.2/wp-includes/… should definitely have the anchor if a URL is available. – Mark Davidson Jul 31 '13 at 10:45
  • 1
    No problem full credit to @Johan for the suggestion. – Mark Davidson Jul 31 '13 at 11:08

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.