Forums

Author Link in a Comment (7 posts)

  1. asechrest
    Member
    Posted 8 months ago #

    Howdy. Havin' a bit of trouble with this one.

    I use a custom author.php. Currenty I use the_author_posts_link() to create a link to the author's page in index and single post views.

    I'm looking for a similar function to link to a comment author's page, but I haven't found a similar function. It appears that comment_author_link() creates a link to the author's personal URL if they provided one, rather than to an author.php.

    Does a function like this exist for comment authors?

    Thanks. :)

  2. asechrest
    Member
    Posted 7 months ago #

    If anyone comes across this thread, I came up with a solution - Linking a Comment Author to an Author Page.

    WordPress provides a function to generate a comment author link, but unlike the function that generates a post author link, the comment author link automatically links to whatever the user has put as their external URL.

    This code allows you to link to the author page. Suggestions on how to improve the code are welcomed. Thanks.

  3. greenshady
    Member
    Posted 7 months ago #

    Oooh, I really don't like solutions that make people change template files. A cleaner approach would be to use the comment_author hook.

    Leave the function <?php comment_author(); ?> in your template and add this to your functions.php

    add_filter( 'comment_author', 'my_site_comment_author' );
    
    function my_site_comment_author( $author ) {
    	global $comment;
    
    	if ( $comment->user_id )
    		$author = '<a href="' . get_author_posts_url( $comment->user_id ) . '">' . $author . '</a>';
    
    	return $author;
    }
  4. asechrest
    Member
    Posted 7 months ago #

    Ahhh, thanks Greenshady! Filters are outside my current understanding, but I'm working on getting there.

    Really appreciate this. I'm gonna go through your code piece by piece to make sure I know what it's doing.

    With your permission, I'd like to add your block of code as an addendum to my blog post. Would have preferred not to edit the template file to begin with, but that was all I could come up with.

    P.S. - Have used the advice from your website on widgetizing the main content sections of my theme in the past. Thanks for that.

  5. greenshady
    Member
    Posted 7 months ago #

    Sure, you can use the code in your post. My code above also assumes that <?php comment_author(); ?> is being used. There are several different ways to call the comment author in WP, so the solution might not work for everyone's theme.

    If you ever want to learn more about WP hooks, check out this site:
    http://adambrown.info/p/wp_hooks

  6. asechrest
    Member
    Posted 7 months ago #

    Saved that link, thanks. It's going to come in handy as I keep digging into PHP and WP.

    And just so I have this right, the primary reason(s)/advantage(s) for using the hook over adjusting the template file is what, exactly? (Other than it just seems cleaner.)

    If I auto-upgrade my theme, would this functionality remain, or would it overwrite my functions.php file?

  7. greenshady
    Member
    Posted 7 months ago #

    If this is in your theme's functions.php, it will get overwritten with an auto-upgrade, but this is easily avoidable for the most part (except for one line of code). So, the best thing to do is put all of your custom functions into a separate PHP file. Chris Pearson has a great article explaining the benefits of a custom functions file:
    http://www.pearsonified.com/2008/05/how-to-use-wordpress-functions.php

    A great reason for this is that you can keep all of your custom stuff in one file and transfer it from theme to theme and not have to worry too much about it in the future.

    I personally prefer working in the realm of child themes, but that's a topic for another day.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.