First recent post missing on custom 404 page


I'm trying to make a custom 404 page that displays recent posts in a grid, but there's an empty div where the first post should be. Thanks in advance for any help.

<?php

beans_modify_action_callback( 'beans_no_post', 'recent_posts_grid' );

function recent_posts_grid() {

  // Get the 3 latest posts
  $posts = get_posts( array( 'numberposts' => 3 ) );

  // Remove post meta
  beans_remove_action( 'beans_post_meta' );

  // Add responsive grid
  ?><div class="uk-grid"><?php

  // Setup the postdata.
  foreach ( $posts as $post ) {

    global $post;

    setup_postdata( $post );

    ?> <div class="uk-width-large-1-3 uk-width-medium-1-2"><?php

      beans_post_title();

      beans_post_image();

    ?></div> <?php

  }

  ?> </div> <?php

}

// Load the document
beans_load_document();

Moving the global variable out of the loop works.

function recent_posts_grid() {

  // Get latest posts
  $custom_posts = get_posts( array( 'numberposts' => 6 ) );

  // Remove post meta
  beans_remove_action( 'beans_post_meta' );

  // Add responsive grid
  ?><div id="recent-posts-grid" class="uk-grid uk-grid-match" data-uk-grid-margin=""><?php

  // Global variable 
  global $post;

  // Add posts
  foreach ( $custom_posts as $post ) {

    // Setup the postdata.
    setup_postdata( $post );
    ?><div class="uk-width-large-1-3 uk-width-medium-1-2">
    <article class="uk-article uk-panel-box"><?php

    // Add post image and title
    beans_post_image();
    beans_post_title();

    ?></article></div><?php

  }

  ?></div><?php

}

Write a reply

Login or register to write a reply, it's free!