Home Forums Front-end Issues I recive value for only 1,3,5,etc. post


I recive value for only 1,3,5,etc. post

  • Hi!
    I have this loop:

    <?php $countOffers = wp_count_posts( 'offer' )->publish; ?>
    <?php $counter = 1; ?>
    <section class="offer-list">
        <div class="container">
                $args = array( 'post_type' => 'offer', 'posts_per_page' => 50 );
                $the_query = new WP_Query( $args ); 
            <?php while ( $the_query->have_posts() && $counter <= $countOffers ) : $the_query->the_post(); ?>
                <?php if ( $counter % 2 != 0 ): ?>
                    <?php get_template_part( 'template-parts/Offer/content', 'left' ); ?>
                <?php else: ?>
                    <?php get_template_part( 'template-parts/Offer/content', 'right' ); ?>
                <?php endif ?>
                <?php ++$counter; ?>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </div><!-- /.container -->

    file content-xxx.php – in both files, I have changed only order of fields

    <div class="offer-item left-side">
        <div class="row vertical-align">
            <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
                <img src="<?php echo get_field( "offer_thumbnail" ); ?>" alt="" class="img-responsive pull-right" />
            </div><!-- /.col-lg-5 -->
            <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
                <div class="item-content">
                    <div class="item-heading">
                        <h1><?php the_title(); ?></h1>
                    </div><!-- /.item-heading -->
                    <div class="item-description">
                            <?php the_content(); ?>
                    </div><!-- /.item-description -->
                    <div class="item-offer-range">
                        <h3>Zakres usług</h3>
                            <?php query_posts('cat=' . get_field( "offer_service-category" ) . ''); ?>
                            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                                    <a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>">
                                        <?php the_title(); ?>
                            <?php endwhile; endif; ?>
                            <?php wp_reset_query(); ?>
                    </div><!-- /.item-offer-range -->
                </div><!-- /.item-content -->
            </div><!-- /.col-lg-7 -->
        </div><!-- /.row -->
    </div><!-- /.offer-item -->

    Problem is with field: get_field( “offer_thumbnail” ) – I getting value only for 1, 3, 5, 7, etc. posts so not for all – why? (the others fields works fine)

  • More than likely this has to do with your use of wp_reset_postdata() and wp_reset_query().

    Both of these function reset the query to information about the “Main Query” not to the “Previous Query”.

    So every time you call wp_reset_query() in the left or right template part you are resetting to the “Main Query” which is probably causing issues in loop in the first part of your code.

    Also this line in your template part
    query_posts('cat=' . get_field( "offer_service-category" ) . '');
    may be overwriting the posts in the “Main Query”, though I’m not sure about that to be honest.

    Anyway, the problem is in the way the queries are nested and the use of resets. Basically, what you’re going to need to do is find another way to loop through the 3rd level query in you’re left and right template parts.

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

You must be logged in to reply to this topic.