0

The single.php page I am working on was able to display two loops successfully, the first being the actual page's post, and the second loop displaying the last 5 posts. However, since I have styled the page things have gone wrong. Now the second loop only ever displays the very last post. When I inspect the backend of the page, it shows that the post page is running every other post from the last 5 posts. For example, if the last 5 posts are 228, 227, 226, 225, and 224, the page is running just 228, 226, and 224. (What happened to 227 and 225?) However, even though 228, 226, and 224 are running, only 228 is showing.

I have checked the Reading section of the site and have it clearly marked to display 5 posts, yet it still won't display anything other than that last post.

Any thoughts?

    <div id="blogcontainer">
    <?php query_posts('post_type=post') ?>
    <?php if (have_posts()) : while(have_posts()) : the_post();?>
    <div class="postcontainer" id="post-<?php the_ID(); ?>">
<div class="thumb">
                <?php if ( has_post_thumbnail()) : ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail(array(100,100)); ?>
            </a>
        <?php endif; ?>
        </div>
  <div class="topblock"><div class="titleblock">    
  <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php 
  the_title(); ?></a></div>
  <div class="moreblock">MORE ></div>
  </div>
  <div class="block">
  <div class="date"><p><span class="date"><?php the_time('F j, Y'); ?></span> 
  </p></div>
    <?php html5wp_excerpt('html5wp_index'); ?>
</div>

        <?php endwhile; ?>

        <?php else: ?>

        <?php endif; ?>
</div>
            </div>
</div>

Here is the page single post

  • 2
    Don't use query_posts, it overwrites the main query and can produce unexpected results. Use WP_Query instead. – Milo Nov 29 '18 at 4:20
  • 1
    please use wp_reset_query(); see codex link:codex.wordpress.org/Function_Reference/wp_reset_query – vikrant zilpe Nov 29 '18 at 9:38
  • vikrant zilpe, I did not include it in the original post, but I did try wp_reset_query once before and it actually made things worse. It made it so that the post displaying in the second loop was the same one on the loop in the main part of the page. My reasoning was that at least with how it is now, it is showing the most recent post from the entire site. – user5258035 Nov 30 '18 at 16:32
1

The answer to the above question was very simple and answered by a kind person at the Wordpress Support forum. There was an extra div that was causing problems. Remove the last div, and it all worked out fine. A very simple solution, but one I wouldn't have known that could cause that much trouble. I hope anyone else looking for a similar solution checks on the divs, because it is very important that the endif statements do not get separated off from their intended function. In this case, the extra div was responsible.

The new code looks like this:

<div id="blogcontainer" onmouseover="RollOff1()" onmouseout="RollOff2()">
<?php query_posts('post_type=post') ?>
    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="postcontainer" id="post-<?php the_ID(); ?>">
<div class="thumb">
            <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php the_post_thumbnail(array(100,100)); // Declare pixel size you need 
            inside the array ?>
        </a>
    <?php endif; ?>


    </div>
   <div class="topblock"><div class="titleblock">   <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
 <div class="moreblock"><a href="<?php the_permalink(); ?>">MORE ></a></div>
  </div>
  <div class="block">
  <div class="date"><p><span class="date"><?php the_time('F j, Y'); ?></span></p> 
  </div>
<?php html5wp_excerpt('html5wp_index'); ?>
  </div>
                                   </div>
    <?php endwhile; ?>

    <?php else: ?>

            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>

    <?php endif; ?>
    </div>

   </div>



  <?php get_footer(); ?>

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.