0
votes

I would like to add to my theme some links so that if a user is logged in, he may go to his own page which lists his own comments and posts.

I have found several functions to help me create the template parts. For example:

How to display logged in users comments (only) on Wordpress

Comment Author Link on Wordpress

The problem is that I cannot find any information on creating the link. It seems like all of the functions for the_author do not accept an ID, such as current user ID. i would like to know how to create links outside the loop, to retrieve a template part, such as current user's comments and posts. Can I create a new _link() function? A search of the codex for _link() isn't returning what I need.

So far I managed to return the actual post titles, but I want a link at the top of my page that says "My Recent Activity", something like that.

Thanks.

1

1 Answers

0
votes

Could you not create a page template called something like user activity. Then query the logged in user's username and use the functions you found to return their comments? So the link would be the same for every user. The page itself would do all the magic by querying based on their username. Here's some code I used to just retrieve recents comments on a home page:

<?php

$recent_comments = get_comments( array(
'number'    => 2,
'status'    => 'approve'
) );
echo '<ul>';
foreach($recent_comments as $c){
    $the_comment = mb_strimwidth($c->comment_content, 0, 80, "...", "UTF-8");
    echo '<li>';
        echo '<p>'.$the_comment.'</p>';
        echo '<span class="comment-source">'.$c->comment_author.'</span>';
        echo '<span class="time-ago">'.$c->comment_date_gmt.'</span>';
        $permalink = get_permalink( $c->comment_post_ID );
        echo '<a href="'.$permalink.'" class="post-link">'.$permalink.'</a>';
    echo '<l/i>';
}
echo '</ul>';
?>

So you can use the parameter "post_author" to query the current user's comments. And use get_currentuserinfo() to get the current user's info.

Hope that's what you're after!