$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>themes - Second Loop Showing Only One Post on Single Post Page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

themes - Second Loop Showing Only One Post on Single Post Page

matteradmin9PV0评论

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

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

Share Improve this question edited Nov 29, 2018 at 7:43 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 29, 2018 at 0:37 user5258035user5258035 113 bronze badges 3
  • 2 Don't use query_posts, it overwrites the main query and can produce unexpected results. Use WP_Query instead. – Milo Commented Nov 29, 2018 at 4:20
  • 1 please use wp_reset_query(); see codex link:codex.wordpress/Function_Reference/wp_reset_query – vikrant zilpe Commented Nov 29, 2018 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 Commented Nov 30, 2018 at 16:32
Add a comment  | 

1 Answer 1

Reset to default 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(); ?>
Post a comment

comment list (0)

  1. No comments so far