WordPress.org

Ready to get started?Download WordPress

Forums

ajax request with permalinks on breaks wordpress loop (2 posts)

  1. stondin
    Member
    Posted 5 years ago #

    Hey Guys,
    We have a jquery carousel installed on our wordpress home page.
    The images for the carousel come via an ajax call from an array which is created using php after cycling through the wordpress posts.

    Without permalinks turned on the carousel works great.

    However after any type of permalink are turned on in wordpress my

     if (have_posts()) :
        while (have_posts()) : the_post();
        endwhile;
     endif;
    

    loop stops working. The page that is loaded via ajax simply no longer receiving any results back.

    The following is the php file that is loaded using a jquery ajax call.

    <?php
    require('../pcd/wp-blog-header.php'); 
    
    // Array indexes are 0-based, jCarousel positions are 1-based.
    $first = max(0, intval($_GET['first']) - 1);
    $last  = max($first + 1, intval($_GET['last']) - 1);
    
    $length = $last - $first + 1;
    
    // ---
    
     if (have_posts()) :
        while (have_posts()) : the_post();
          if (get_custom('featured') == 'true'){
            $details[] = array
            (
             "details" => array
             (
              "image" => get_custom('featured_img'),
              "title" => get_the_title(),
              "link"  => get_permalink(),
              )
            );
          }
        endwhile;
      endif;
    
    $total    = count($details);
    $selected = array_slice($details, $first, $length);
    
    // ---
    
    header('Content-Type: text/xml');
    
    echo '<data>';
    
    // Return total number of images so the callback
    // can set the size of the carousel.
    echo '  <total>' . $total . '</total>';
    
    foreach ($selected as $img) {
        echo '<item>';
        echo '<title>';
        echo $img["details"]["title"];
        echo '</title>';
        echo '<link>';
        echo $img["details"]["link"];
        echo '</link>';
        echo '<image>';
        echo $img["details"]["image"];
        echo '</image>';
        echo '</item>';
    }
    
    echo '</data>';
    
    ?>
    

    Does any one have any ideas? The following link shows our work so far and maybe using firebug you can see the server responce.
    http://wp.paulchang.com

    Many many thanks

    Dan

  2. stondin
    Member
    Posted 5 years ago #

    Incase people have the same problem... I fixed the issue.

    We needed to specify a new query object

    $my_query = new WP_Query('showposts');
    

    He's my final AJAX loop

    $my_query = new WP_Query('showposts');
    
     if ($my_query->have_posts()) :
        while ($my_query->have_posts()) : $my_query->the_post();
          if (get_custom('featured') == 'true'){
            $details[] = array
            (
             "details" => array
             (
              "image" => get_custom('featured_img'),
              "title" => get_the_title(),
              "link"  => get_permalink(),
              )
            );
          }
        endwhile;
      endif;
    

Topic Closed

This topic has been closed to new replies.

About this Topic