Skip to content

Instantly share code, notes, and snippets.

@melissacabral
Last active February 26, 2018 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melissacabral/53cc5f12bc0c94a5a581c6d0d113b94f to your computer and use it in GitHub Desktop.
Save melissacabral/53cc5f12bc0c94a5a581c6d0d113b94f to your computer and use it in GitHub Desktop.
step 1 of customizing comments display
<?php
//hide all of this file if the post if password protected
if( post_password_required() ){
return;
}
//get a distinct count of comments vs. pings
$comm = get_comments( array(
'status' => 'approve',
'post_id' => $id, //This post
) );
$comments_by_type = separate_comments( $comm );
$comment_count = count($comments_by_type['comment']);
$pings_count = count($comments_by_type['pings']);
?>
<?php if( $comment_count ){ ?>
<section class="comments">
<h3><?php echo $comment_count == 1 ? 'One comment' : $comment_count . ' comments'; ?> on this post:</h3>
<ol>
<?php
//just show normal comments
wp_list_comments( array(
'type' => 'comment',
'avatar_size' => 50,
) );
?>
</ol>
<div class="pagination">
<?php previous_comments_link(); ?>
<?php next_comments_link(); ?>
</div>
</section>
<?php } //end if there are comments ?>
<section class="comments-form">
<?php comment_form(); ?>
</section>
<?php if( $pings_count ){ ?>
<section class="pings">
<h3><?php echo $pings_count == 1 ? 'One site' : $pings_count . ' sites' ; ?> mention this post:</h3>
<ol>
<?php
//just show pingbacks and trackbacks
wp_list_comments( array(
'type' => 'pings', //pingbacks and trackbacks
'short_ping' => true,
) );
?>
</ol>
</section>
<?php } //end if there are pings ?>
<?php
//Add this filter to the end of the current functions.php file:
/**
* Fix the Number of comments to only include REAL comments (not pingbacks or trackbacks)
*/
add_filter( 'get_comments_number', 'portfolio_comment_count' );
function portfolio_comment_count(){
//get the current post id
global $id;
$comments = get_approved_comments( $id );
$comment_count = 0;
foreach ( $comments as $comment ) {
//only count it if it is a "normal" comment
if( $comment->comment_type == "" ){
$comment_count++;
}
}
return $comment_count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment