0

I want to show current user comments with the title of the posts in his custom front-end page that I made.

currently I use this code but this only shows the comment itself.

    add_shortcode ( 'show_recent_comments', 'show_recent_comments_handler' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 10,
        "pretty_permalink" => 0
        ), $atts ));

    $output = ''; // this holds the output

    if ( is_user_logged_in() )
    {
        global $current_user;
        get_currentuserinfo();

        $args = array(
            'user_id' => $current_user->ID,
            'number' => $count, // how many comments to retrieve
            'status' => 'approve'
            );

        $comments = get_comments( $args );
        if ( $comments )
        {
            $output.= "<ul>\n";
            foreach ( $comments as $c )
            {
            $output.= '<li>';
            if ( $pretty_permalink ) // uses a lot more queries (not recommended)
                $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
            else

                $output.= '<a href="'.get_settings('siteurl').'/?p='.$c->comment_post_ID.'#comment-'.$c->comment_ID.'">';         
            $output.= $c->comment_content;
            $output.= '</a>';

            $output.= "</li>\n";
            }
            $output.= '</ul>';
        }
    }
    else
    {
        $output.= "<p class='button-com2'>Please login first.</p>";
    }
    return $output;
}

I want to show the title of the post, too.

Any kind of help would be appreciated. Thanks.

0

You can get the title of a post if you have the post ID and every comment is assigned to a post ID. So basically, just use the comment_post_ID + get_the_title() function:

get_the_title($c->comment_post_ID);
  • You're welcome! Vote up the answer if it helped you ;) – levelzwo Dec 18 '14 at 21:25
  • I tried but I can't because voting up needs at lease 15 reputation that I don't have right now. Sorry but Thank you so much. – Kevin Armstrong Dec 21 '14 at 20:49
  • But I think you can somehow mark the answer as "right" because you asked the question :) – levelzwo Dec 22 '14 at 11:44
  • You're welcome, and welcome to Stack Exchange :) – levelzwo Dec 22 '14 at 16:53

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.