2012-09-14 2 views
0

The Loop 외부에 사용자의 총 댓글 수를 표시하려면 어떻게해야합니까?Wordpress의 The Loop 외부에 사용자의 총 댓글 수 표시

나는 루프 내부 의견 수 를 표시하려면이 코드를 사용

루프 내에서 잘 작동
<?php 
    global $wpdb; 
    $user_id = $post->post_author; 
    $where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ; 
    $comment_count = $wpdb->get_var(
     "SELECT COUNT(*) AS total 
    FROM {$wpdb->comments} 
    {$where} 
"); 
    echo 'Comments: <strong>' . $comment_count . '</strong>'; 
    ?> 

. 그 코드가 루프 외부에서 작동하도록하기 위해 $user_id = $post->post_author;$user_id = get_the_author_meta('ID');으로 변경했지만 작동하지 않았습니다.

내가되었다는 가까운이 코드입니다 :

      <?php 
          global $wpdb; 
          $where = 'WHERE comment_approved = 1 AND user_id <> 0'; 
          $comment_counts = (array) $wpdb->get_results(" 
           SELECT user_id, COUNT(*) AS total 
           FROM {$wpdb->comments} 
           {$where} 
           GROUP BY user_id 
           ", object); 
          foreach ($comment_counts as $count) { 
           $user = get_userdata($count->user_id); 
           echo 'Comments: ' . $count->total . ' 
           '; 
          } 
          ?> 

그러나,이 같은 모든 사용자에 대해이 에코 코멘트 수, "댓글 : 28 개 댓글 : 11 개 댓글 : 55"등

에 어떤 코드를 사용하면 루프 외부에서 사용자의 의견 수가 표시됩니까?

+0

게시물 외부에서 사용하는 경우'$ post'를 global로 만들어야합니다 .. – SMacFadyen

+0

** global $ wpdb; **를 ** global $ wpdb로 변경했습니다. 첫 번째 코드에서 $ post; **를 변경하고 ** $ user_id를 변경했습니다. = $ post-> post_author; ** to ** $ user_id = get_the_author_meta ('ID'); ** 그러나 작동하지 않았습니다. 나는 당신의 해결책에 따라 어떤 잘못을 했습니까? –

답변

4

몇 가지 사항을보다 포괄적으로 만들고 사용자 데이터를 다르게 가져보십시오.

<?php 

global $wpdb, $post, $current_user; 
get_currentuserinfo(); 
$userId = $current_user->ID; 

$where = 'WHERE comment_approved = 1 AND user_id = ' . $userId ; 
$comment_count = $wpdb->get_var("SELECT COUNT(*) AS total 
           FROM {$wpdb->comments} 
           {$where}"); 
echo 'Comments: <strong>' . $comment_count . '</strong>'; 
?> 

또는 functions.php에서

<? 
function commentCount() { 
    global $wpdb, $current_user; 
    get_currentuserinfo(); 
    $userId = $current_user->ID; 

    $count = $wpdb->get_var(' 
      SELECT COUNT(comment_ID) 
      FROM ' . $wpdb->comments. ' 
      WHERE user_id = "' . $userId . '"'); 
    echo $count . ' comments'; 
} 
?> 

는 ... 그것은

<?php commentCount(); ?> 
+0

이 코드는 작동하지만 오류가 발생했습니다. wp-admin 백엔드 (플러그인 용)에있는이 페이지에서이 오류가 표시되었습니다. ** 경고 : foreach() **에 잘못된 인수가 제공되었습니다. 그 줄을 확인하고 포함되어 있습니다 : ** foreach ($ post as $ post) {** 어쨌든이 충돌하지 않도록 코드를 변경할 수 있습니까? –

+0

방금 ​​Global에서 $ post를 제거했고 모든 것이 오류없이 작동합니다. 왜 그것이 $ 게시물없이 작동하는지 아십니까? 나는 단지 내가 호기심이 많기 때문에 라이브 사이트에서 사용하기 전에 이것이 무엇을하는지 이해하고있다. –

+0

아 맞아. 그것은 백엔드입니다! 그 이유를 설명 할 것입니다. :) – SMacFadyen

0

당신은 자신의 프로필을보고하는 경우 사용자 당 코멘트를 얻을 수 WHERE comment_author_email = "' . get_comment_author_email() . '"을 사용해야 코드를 호출하려면 @SMacFadyen은 로그인 한 사용자에게 코멘트 수만 줄 것입니다.

게다가 두 번째 기능이 작동하지 않습니다.

0

나는이 주제는 오래 알고이 일

$post= get_post($id=5); 
$comment_count = $post->comment_count; 
0

시도하지만, 단지 훨씬 더 적합한 조각, h 제하려

<?php 
$args = array(
    'post_id' => 1, // use post_id, not post_ID 
     'count' => true //return only the count 
); 
$comments = get_comments($args); 
echo $comments 

?> 

하는 'post_id를'가 'user_id를'할 수있다