1

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.

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; ?>
2
  • I dont have much experience with ajajx wordking with wordpress so that why im stuck Commented Feb 19, 2012 at 20:42
  • ok the real problem is the same content keep on being returned even if you visit the post without the hash Commented Feb 25, 2012 at 15:53

1 Answer 1

3

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

that wp_reset_postdata part saved my night! damnit they say nothing about this in the wordpress documentations on the_lopp. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.