• Hi guys, i’m needing some help! I’m using this code to show a custom post list, but looks like setup_postdata is not setting up my last post’s date idk why. I currently have 3 posts created, two of them working properly, but the first post created is no getting the_date() from setup_datapost.

    Can anyone help me? I’ve looked over here and google i haven’t found any useful insight about this.

    Thanks!

    <?php
    /*
      Template Name: Artículos
     */
    get_header();
    
    $args = ['order' => 'DESC', 'orderby' => 'date'];
    $postslist = get_posts($args);
    ?>
    
    <?php if ($postslist):
            global $post; ?>
            <section id="articles_header" class="articles-header conf-header">
                <div class="container-fluid flex-col flex-bottom height-100">
                    <div class="row flex-row flex-bottom header-title-margin">
                        <div class="col-xs-10 col-lg-11 col-xs-offset-2 col-lg-offset-1 conf-header-line">
                            <span class="conf-header-tittle">
                                <h1>Artículos</h1>
                            </span>
                        </div>
                    </div>
                </div>
            </section>
            <section id="articles_list" class="articles-list-section">
                <div class="container">
                    <div class="row" role="main">
                        <?php
                        foreach ($postslist as $post):
                            setup_postdata($post);
                            $feat_image = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));
                            ?>
                            <a href="<?php the_permalink(); ?>">
                                <div class="col-xs-5 col-md-5 col-xs-offset-1 col-md-offset-1 post-square-link" style="background: url('<?php echo ($feat_image !== false ? $feat_image : ''); ?>')">
                                    <div class="row">
                                        <div class="col-xs-9 blue-box">
                                            <div class="row">
                                                <h5 class="blue-box-title"><?php the_title(); ?></h5>
                                            </div>
                                            <div class="row">
                                                <div class="col-xs-6 flex-row"><h6 class="article-date"><?php the_date(); ?></h6></div>
                                                <div class="col-xs-6 flex-row"><img class="little-right-arrow" src="<?php bloginfo('template_url'); ?>/img/little-right-arrow.png"></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </a>
                        <?php endforeach;
                            wp_reset_postdata();
                        ?>
    
                    </div>
                </div>
            </section>
    <?php endif; ?>
    
    <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • Your code looks clean to me.

    Were two of your posts posted on the same day? If so, this reference from https://codex.wordpress.org/Function_Reference/the_date that might help:

    SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string.
    Use <?php the_time( get_option( ‘date_format’ ) ); ?> to add the date set in the admin interface.

Viewing 1 replies (of 1 total)
  • The topic ‘setup_postdata no setting up last post's data’ is closed to new replies.