WordPress loop inside a loop not working correctly

OK here goes, I’m using jQuery ajax to load post from inside a slider. In wordpress dashboard I set the number of post per page to “one”.

The problem I’m having is only the latest post created keeps on being loaded. Also sometimes the inner loop keep on going forever.

Read More

I need whatever link from the slider, when clicked, to simply load the post inside the content area I set, Below is all the relevant code.

The slider code

<ul id="roundabout" class="clearfix">

<?php  $argss = array(
"showposts" =>20);

query_posts($argss);  ?>


    <?php while (have_posts()): the_post(); ?>

       <li><a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(150, 150, true));?></a></li> 
    <?php endwhile; ?>

</ul>

The jQuery ajax code

    var $mainContent = jQuery(".content"),
    siteUrl = "http://" + top.location.host.toString(),
    url = ''; 

jQuery(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() {
    location.hash = this.pathname;

    return false;
}); 

jQuery(window).bind('hashchange', function(){
    url = window.location.hash.substring(1); 

    if (!url) {
        return;
    } 

    url = url + " .content"; 

    $mainContent.fadeOut().load(url, function() {
        $mainContent.fadeIn();
    });
});

jQuery(window).trigger('hashchange');

The PHP code

<?php while ( have_posts() ): the_post(); ?>

    <div class="content">    

        <?php wp_reset_query(); ?>

            <?php while(have_posts()): the_post(); ?>

                <div id="inner">
                    <h2 class="title"><?php the_title(); ?></h2>
                    <?php the_post_thumbnail(array(150, 150, true)); ?>
                    <?php the_content(); ?>  
                </div>

                <?php endwhile; ?>

      <div class="clear"></div>

    </div>

<?php  endwhile; ?>

Related posts

Leave a Reply

1 comment

  1. take a look at WP_Query in the codex: http://codex.wordpress.org/Class_Reference/WP_Query

    Your ‘loop inside a loop’ wont work as you’ve structured here because there is no second query. Try using WP_Query inside the first main loop to get the posts for your second loop

    <?php while ( have_posts() ): the_post(); ?>
    
    <div class="content">    
    
        <?php
    
        // Second Query
        $the_query = new WP_Query( $args );
    
        // Second Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
    
        ?>
    
                <div id="inner">
                    <h2 class="title"><?php the_title(); ?></h2>
                    <?php the_post_thumbnail(array(150, 150, true)); ?>
                    <?php the_content(); ?>  
                </div>
    
          <?php endwhile;
    
          // Reset Second Loop Post Data
          wp_reset_postdata(); 
    
          ?>
    
      <div class="clear"></div>
    
    </div>
    
    <?php  endwhile; ?>
    

    This should help solve half of your problem 🙂