0

Hi I'm using this code below to display five posts from a specific tag. Also the code allows me to stlye the first post different from the next four.

<?php
$queryObject = new  Wp_Query( array(
'showposts' => 5,
'post_type' => array('post'),
'tag_slug__and' => array('featured-music'),
'orderby' => 1
));
$i = 0;
$class = array("first-post", "posts-below");
while ( $queryObject->have_posts() ) {
    $queryObject->the_post();
    if ( $i == 0 ) { ?>
        <div class=<?php echo $class[0]; ?>>
            <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ),     wp_specialchars(get_the_title(), 1)) ?>">
                <?php the_post_thumbnail('sidethumbs'); ?>
            </a>
        </div>
    <?php 
         $i++;
     } else { ?>
        <div class=<?php echo $class[1]; ?>>
            <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ),     wp_specialchars(get_the_title(), 1)) ?>">
                <?php the_post_thumbnail('sidethumbs'); ?>
                <?php the_title(); ?>    
            </a>
        </div>
    <?php $i++;
 } 


} ?>

This code is placed inside a template file called "hot-music.php". I call for the output of template page in my main index file, between the 8th and 9th post.

Now the problem I am having is that the last post that is in the "hot-music.php" file repeats itself inside the main index loop. It's somehow makes the replaces the 9th post in the regular loop with the last post inside the "hot-music.php" template.

Here is how my loop is set up --> https://gist.github.com/mihadaiko/dc314a845f807c16d19db8c842ed7a75

and here is how I insert the code above, by calling the template page it's located in.

 <?php elseif (8 < $count && $count <= 17 && $paged === 1) :
       if ($count === 9) echo '<div style="clear:both;"></div><div class="row div-break-first" style="margin:10px 0px 50px 0px;">' ,get_template_part('hot','music'), '</div>'; ?>

But for some odd reason it throws my regular loop off by one count. The normal post are suppose to continue in order after this code but instead the last post from the loop above repeats itself one more time inside my main index loop. Is there a fix for this.

0

You did not include wp_reset_postdata(); after the while loop.

  • Hi Emranul, welcome to WPSE and thank you for your answer, which seems to have helped the OP. Could you please edit your answer though to expand on why this is the solution, to help others that come across this? Thanks! – Tim Malone Aug 1 '16 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.