$end error in code

I’m receiving “Parse error: syntax error, unexpected $end in…on line 142”. Here’s my code:


<?php if ( have_posts() ) {    while ( have_posts() ) { the_post(); ?>
        <?php if ( in_category( '4' ) ) : ?>
<div id="bloglatest">
             <div class="container">
  <pre><code><?php
    
             $postslist = get_posts('numberposts=3&order=DESC&orderby=date');
    
             foreach ($postslist as $post) :
    
             setup_postdata($post);
    
            ?></code></pre>

        

 <div class="bloglatest col-lg-4 col-md-4 col-sm-6 col-xs-12">
        
                 <span class="date"><?php the_time(get_option('date_format')) ?></span>
        
                 <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

    
                 <pre><code><?php                                
                     if ( has_post_thumbnail() ) {
                     the_post_thumbnail();
                     }
                     the_excerpt(); ?></pre></code>
            

<p class="readmore"><a href="<?php echo get_permalink($recent_post["ID"]); ?>">Continue Reading</a></p>
        
             </div>

         <pre><code><?php endforeach; ?>        </code></pre>

       </div>
</div>
<?php endif; ?>

Line 142 is <?php get_footer(); ?> which is located at the last line, but I’m figuring I’m missing a bracket somewhere.

You didn’t close your foreach loop. That’s why I never mismatch styles when I’m coding.

I personally keep the bracket style going and would use this:

             foreach ($postslist as $post) {
                setup_postdata($post);
             }

Thanks, DaveMaxwell, but I got this error:
Parse error: syntax error, unexpected T_ENDFOREACH in on line 143

Try this:

<?php
if ( have_posts() ) :
  while ( have_posts() ) : 
      the_post();
      if ( in_category( '4' ) ) :
         echo '<div id="bloglatest"><div class="container">';
          echo '<pre><code>';
            $postslist = get_posts('numberposts=3&order=DESC&orderby=date');
            foreach ($postslist as $post) :
              setup_postdata($post);
              $blurb();
            endforeach;
          echo '</code></pre>';
    echo '</div></div> // container AND bloglatest ';
endif; // in_category('4')

endwhile;
endif; // have_posts()

//==============
function blurb()
{
?>    
  <div class="bloglatest col-lg-4 col-md-4 col-sm-6 col-xs-12">
    <span class="date"><?php the_time(get_option('date_format'));?></span>
    <h3>
      <a href="<?php the_permalink(); ?>">
        <?php the_title();?>
      </a>
    </h3>

    <pre><code>
      <?php                                
        if ( has_post_thumbnail() )
        {
          the_post_thumbnail();
        }
        the_excerpt();
      ?>
    </code></pre> <?php // CORRECTED by reversing </pre></code> ?>

    <p class="readmore">
      <a href="<?php echo get_permalink($recent_post["ID"]); ?>">
        Continue Reading
      </a>
    </p>
  </div>
<?php  
} // end blurb()

Also have you really got an id and a class named bloglatest?

Thanks, John_Betong.

Your code hides the containers if there are no posts (which is good), but no posts display when there are published posts (which is bad).


And, yes, I do have both a class and an ID of the same name. This is just for demo purposes.

At the top of your script set the following:
–PHP
error_reporting(-1);
ini_set('display_errors, true);

Also pass the $post variable to the blurb() function and in the blurb () function view the variable using the following:

var_dump( $post );

die;

// this is hard work trying to post from a tablet

Unable to surround var dump with pre tags. Will edit again when on the desktop.

Thanks, John_Betong. I’ll give it a whirl and see what happens.

Try this:

// replace the loop with this script
echo '<pre><code>';
  $postslist = get_posts('numberposts=3&order=DESC&orderby=date');
  // v($postslist);die; // unrem to view
  foreach ($postslist as $post) :
    // v($post);die;  // unrem to view
    setup_postdata($post);
    // v($post);die;  // unrem to view
    $blurb($post);
  endforeach;
echo '</code></pre>';

//===================
function blurb($post)
{
// v($post); die;  // unrem to view
?>    
  <div class="bloglatest col-lg-4 col-md-4 col-sm-6 col-xs-12">
    <span class="date"><?php the_time(get_option('date_format'));?></span>
    <h3>
      <a href="<?php the_permalink(); ?>">
        <?php the_title();?>
      </a>
    </h3>

    <pre><code>
      <?php                                
        if ( has_post_thumbnail() ):
          the_post_thumbnail();
        endif;
        the_excerpt();
      ?>
    </code></pre> <?php // CORRECTED by reversing </pre></code> ?>

    <p class="readmore">
      <a href="<?php echo get_permalink($recent_post["ID"]); ?>">
        Continue Reading
      </a>
    </p>
  </div>
<?php  
} // end blurb()

// Debug function
function v($var)
{
  echo '<pre>';
    var_dump($var);
  echo '</pre>';
}

Sorry, John_Betong, but that still leaves a container displaying when there are no posts.

If there are posts to display, it comes up with the error:
Fatal error: Function name must be a string on line 118

Maybe you can explain to me why you’re creating a function if all I need to do is to keep the thought of using a simple loop with the idea of “If there are posts in category 4, then display them and with this styling container”?

My mistake, please remove the $ preceding $blurb($post);

It is difficult to debug when only a small section of the script is available.

The idea of using a function was to simplify the nested loops which were incorrect, also to help debugging.

Once the function is working OK the script inside the function can be copied and pasted inside the original loop. I would personally keep the function because it is easier to make another copy of the function to simplify output or to optimise the output.

foreach ($postslist as $post) :
    setup_postdata($post);
    if( false ):
      blurb($post); // original version
    else:
      blurb_002($post); // test
    endif;
  endforeach;

I’ve managed to get it working: