Home > Software engineering >  Wordpress while loop with current_post
Wordpress while loop with current_post

Time:01-14

I've created an ajax callback where I display posts with specific tags, which works fine. But I want to display the 5 posts with a different DIV surrounding each and I cannot get it to work.


    function filter_projects() {
      $tagSlug = $_POST['tags'];

      if(!empty($tagSlug)) {
        
        $ajaxposts = new WP_Query(array(
          'post_type' => 'projects',
          'posts_per_page' => 5,
          'order_by' => 'date',
          'order' => 'desc',
          'tax_query' => array(
              array(
                  'taxonomy' => 'tag',
                  'terms' => $tagSlug,
                  'field' => 'slug',
              )
          ),
        ));
        
      } else {
        
        $ajaxposts = new WP_Query(array(
          'post_type' => 'projects',
          'posts_per_page' => 5,
          'order_by' => 'date',
          'order' => 'desc',
        ));
        
      }

        $response = '';

      


      if ( $ajaxposts->have_posts() ) :     
          while ( $ajaxposts->have_posts() && $ajaxposts->current_post = 0 ) : $ajaxposts->the_post(); 
            $template = include('templates/index-case.php');         
            $response .= '<div >' . $template . '</div>';
          endwhile; 
      endif;

      if ( $ajaxposts->have_posts() ) :     
          while ( $ajaxposts->have_posts() && $ajaxposts->current_post = 1 ) : $ajaxposts->the_post(); 
            $template = include('templates/index-case.php');         
            $response .= '<div >' . $template . '</div>';
          endwhile; 
      endif;

      if ( $ajaxposts->have_posts() ) :     
          while ( $ajaxposts->have_posts() && $ajaxposts->current_post = 2 ) : $ajaxposts->the_post(); 
            $template = include('templates/index-case.php');         
            $response .= '<div >' . $template . '</div>';
          endwhile; 
      endif;

      if ( $ajaxposts->have_posts() ) :     
          while ( $ajaxposts->have_posts() && $ajaxposts->current_post = 3 ) : $ajaxposts->the_post(); 
            $template = include('templates/index-case.php');         
            $response .= '<div >' . $template . '</div>';
          endwhile; 
      endif;

      if ( $ajaxposts->have_posts() ) :     
          while ( $ajaxposts->have_posts() && $ajaxposts->current_post = 4 ) : $ajaxposts->the_post(); 
            $template = include('templates/index-case.php');         
            $response .= '<div >' . $template . '</div>';
          endwhile; 
      endif;
      

      echo $response;
      exit;
    }
    add_action('wp_ajax_filter_projects', 'filter_projects');
    add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');
    

I've tried lots of different syntax things, but nothing seems to work. I only get displayed the first post.

The output is suppose to be like this:

    <div >Post One</div>
    <div >Post Two</div>
    <div >Post Three</div>
    <div >Post Four</div>
    <div >Post Five</div>

Can someone help me out?

Thanks, Andreas

CodePudding user response:

Did you check how many posts are returning from your DB. use var_dump

var_dump($ajaxposts);

and check. If it returns 5 post then you have problem in your display code. You must use loop to display the records. For example

if ( $ajaxposts->have_posts() ) {
    while ( $ajaxposts->have_posts() ) {
        $ajaxposts->the_post();
        // display content
    }
}

CodePudding user response:

Make sure you do var_dump($ajaxposts) to see if your WP_Query is returning the posts that you're expecting.

I'm not sure why you're looping through your WP_Query multiple times while what you're trying to achieve can be accomplished by using a simple variable that counts up as you're looping through:

    $count=0;
$template = include('templates/index-case.php');   

if ( $ajaxposts->have_posts() ) {
    while ( $ajaxposts->have_posts() ) {       
        $ajaxposts->the_post() ;  
            $count  ;  
            $response .= '<div >' . $template . '</div>';
    } // end while
} // end if

Alternatively, should you wish to assign specific names you can use the same count variable to loop through an array with names:

$div_names=array('one','two','three','four','five');
$count=0;
$template = include('templates/index-case.php');   

if ( $ajaxposts->have_posts() ) {
    while ( $ajaxposts->have_posts() ) {       
        $ajaxposts->the_post() ;  
            $response .= '<div >' . $template . '</div>';
            $count  ;  
    } // end while
} // end if
  • Related