Support

Account

Home Forums General Issues Using post object in a loop of post

Solved

Using post object in a loop of post

  • Im trying to follow some of your post and some issues that was solved here but im still seeing repeated title in my each post.

    So I have this code:

    <?php
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $post_article = new WP_Query([ 'post_type' => ['post'], 'paged' => $paged, 'pagination' => true ]);
    
    while ( $post_article->have_posts() ) : $post_article->the_post();
    
    $category = get_the_category();
    $attached = get_field('featured_image');
    $excerpt  = get_field('post_excerpt'); ?>
    
      <figure class="author-thumb">
      <?php
        $post_objects = get_field('post_author');
        if( $post_objects ) :
          foreach( $post_objects as $post ) : setup_postdata( $post );
            $avatar = get_field('featured_image', $post_object->ID);
            echo wp_get_attachment_image( $avatar['id'], [ 40, 40, true ], '', [ 'class' => 'uk-border-circle' ] );
          endforeach; wp_reset_postdata();
        else :
          echo '<img src="../avatar.jpg" alt="">';
        endif;
      ?>
      </figure>
    
      <article>
        <div class="meta-title"><h1><?php the_title(); ?></h1></div>
      </article>
    <?php endwhile; wp_reset_query(); ?>

    Though I can see the output, except for the title… Instead, it repeat the title to other posts.

    Example of original line-up of title:

    Title One | Title Two | Title Three … and so on

    But when I execute the post_objects this is what it looks like:

    Title One | Title One | Title One … and so on

    Note:
    The post_object was pulling another custom post type and wanted to display the avatar only.

  • Try changing:

    endforeach; wp_reset_postdata();

    to:

    endforeach; $post_article->reset_postdata();

    … and let me know.

  • Yeah i found this is the answer to my issue… Thanks Keith.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.