简体   繁体   中英

wordpress page that Shows recent comments on the current user's posts

in my wordpress website i let registerd users to make posts on the website, now i need to make a page that shows the most recent comments on the posts of the current user.

i have found this code ref which shows the comments of the current user not the comments on his posts

 <?php /* Plugin Name: Show Recent Comments by a particular user Plugin URI: http://blog.ashfame.com/?p=876 Description: Provides a shortcode which you can use to show recent comments by a particular user Author: Ashfame Author URI: http://blog.ashfame.com/ License: GPL Usage: */ 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.= "<h2>You should be logged in to see your comments. Make sense?</h2>"; $output.= '<h2><a href="'.get_settings('siteurl').'/wp-login.php?redirect_to='.get_permalink().'">Login Now &rarr;</a></h2>'; } return $output; } ?> 

what can i change in this code to make it gets the comments on the current user posts not his own comments?

The get_comments function on Wordpress has a whole slew of arguments you can pass through it. To search for comments based on the author of a post, rather than the user_id of the comment, you'll want the "post_author" argument -

https://codex.wordpress.org/Function_Reference/get_comments

So, consider changing your $args array accordingly so that it looks for matches where the current_user's ID is the post_author:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM