Link to home
Create AccountLog in
Avatar of Tim Burkart
Tim BurkartFlag for United States of America

asked on

How to format a page using WordPress the_content hook?

Hi,

I'm using the_content WordPress hook to add additional content to a blog page. The code works for the target. However, as hook intercepts all content, I am having a problem on the WordPress pages where shortcodes are not rendered and other formatting does not occur. Is there something missing in my code?

Here is a link to my code - the_content hook

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

For everybody to quickly view the code, this is what is in the linked paste bin.  It will be easier this way.


function display_comment_summaries() {
    $p = get_post();
    $output = '';

    if ( is_front_page() && is_home() ) {
      // Default homepage
      return;
    } elseif ( is_front_page() ) {
        // static homepage
        goto display_the_page;
    } elseif ( is_home() ) {
      // blog page
      goto display_comments;
    } else {
        //everything else
        goto display_the_page;
    }
    display_the_page:
    $output .= $p->post_content;
    goto display_output;
    
    display_comments:
    $num_comments = 5;
    $output .= $p->post_content;
   if (( $p->comment_count ) > 0) {
       $output .= '<p style="font-weight: 700; margin-top: 10px;">Latest Comments</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 ) ) );
               //$output .= '<li>' . $c->comment_content . '</li>';
               $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;
    
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tim Burkart
Tim Burkart
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer