0

I am using the_content filter to append comment summaries to posts on a blog page. That part of my code works. I am having a problem with the display of pages where they are not formatted and shortcodes are not rendered. What am I missing, or doing wrong?

function display_comment_summaries() {
$p = get_post();
$output = '';
$output .= $p->post_content;

if ( is_front_page() && is_home() ) {
  // Default homepage
  return;
} elseif ( is_front_page() ) {
    // static homepage
    goto display_output;
} elseif ( is_home() ) {
  // blog page
  goto display_comments;
} else {
    //everything else
    goto display_output;
}

display_comments:
$num_comments = 5;
if (( $p->comment_count ) > 0) {
    $output .= '<p style="font-weight: 700; margin-top: 10px;">Latest Comments (' . $p->comment_count . ')</p>';
    $output .= '<ul style="list-style-type: none; margin-bottom: 10px; padding-left: 0;">';
    $comments = get_comments( array ( 'post_id' => $p->ID ) );
    if (($p->comment_count) < $num_comments) {
        $num_comments = $p->comment_count;
    }
    $i = 0;
    foreach ( $comments as $c ) { 
        if ($i < $num_comments) {
            $day = ltrim( substr( $c->comment_date, 8, 2 ), '0' );
            $year = substr( $c->comment_date, 0, 4 );
            $month_name = date( "F", strtotime( substr( $c->comment_date, 0, 10 ) ) );
            $date = $month_name . ' ' . $day . ', ' . $year;
            $output .= '<li>' . $c->comment_author . ' on ' . $date . '</li>';
            $i++;
        }
    }
    $output .= '</ul>';

    $output .= '<p><a href="' . get_permalink( $p->ID ) . '#comments' . '">' 
        . 'Read Comments</a></p>';
}
display_output:
echo $output;

}
2
  • 1
    1. Nowhere in your code is there a call to the_content() or get_the_content(), which would actually pull in the post content. ($post->post_content is the raw post content, before the the_content filter gets applied.) 2. Is there a compelling reason for you to use goto instead of functions?
    – Pat J
    Oct 10 at 23:08
  • 1
    Thank you. I figured out the problem and restructured my code.
    – bigmoxy
    Oct 11 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.