2017-12-19 10 views
0

나는 현재 사용자의 게시물에 대한 가장 최근 코멘트를 보여주는 페이지를 만들 필요가있는 사용자를 웹 사이트에 등록하도록 등록했습니다.현재 사용자의 게시물에 대한 최근 코멘트를 표시하는 워드 프레스 내 워드 프레스 웹 사이트의

내가 현재 사용자의 의견

<?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; 
 
} 
 
?>
이 코드에서 변경할 수있는

만들기 위해 자신의 게시물에없는 주석을 보여줍니다이 코드 ref를 발견 그것은 코멘트를 얻을 수 현재 사용자가 자신의 의견을 게시하지 않습니까?

답변

3

Wordpress의 get_comments 함수에는 전달할 수있는 전체 인수가 있습니다. 오히려 주석의 USER_ID보다, 게시물의 저자에 따라 의견을 검색하려면, 당신은 "post_author"인수 할 것 - 이에 따라

https://codex.wordpress.org/Function_Reference/get_comments 그래서

, 당신의 $의 args 배열을 변경하는 것을 고려을 있도록 current_user의 ID가 post_author 인 일치 항목을 찾습니다.

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

완벽한 ... 고맙습니다. –