Advertisement
pjeaje

Conditional: IF current user has NOT commented on current po

Feb 28th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. https://wordpress.stackexchange.com/questions/295188/conditional-if-current-user-has-not-commented-on-current-post/295211
  2.  
  3. <?php
  4. wp_reset_postdata();
  5. $post_id = get_the_ID();          // ID of the current post.
  6. $user_id = get_current_user_id(); // ID of the current user.
  7. $post_author_id = get_post_field( 'post_author', $post_id );
  8. $comment_count = 0;
  9.  
  10. // Get the number of comments made by the current user on the current post.
  11. if ( $user_id && $post_author_id != $user_id ) {
  12.     $comment_count = get_comments( array(
  13.         'post_id' => $post_id,
  14.         'user_id' => $user_id,
  15.         'count'   => true,
  16.     ) );
  17. }
  18.  
  19. // The user has not yet commented on the current post. So do the something.
  20. // Or, the user has already commented; but the post is his or her own post.
  21. if ( ! $comment_count ) { ?>
  22. please leave a response
  23. <?php } else { ?>
  24. thanks for your response
  25. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement