Home > database >  the load more ajax button in wordpress loads already displayed posts, instead of new ones
the load more ajax button in wordpress loads already displayed posts, instead of new ones

Time:02-05

I'm trying to display a list of "hot" products on the main page. by default, 4 products are displayed and 2 new products must be loaded using the "load more" button. when you click on the button, the first line is duplicated with the first two products instead of two new ones. link to the website

front-page.php

 <div  id="ajax-container">



                        <?php
$args = array(
   'post_type' => 'product',
    'posts_per_page' => 4,
    'post_status' => 'publish',
    'meta_key'  => 'hot',
   'meta_value' => true
);
$products = new WP_Query($args);
$max_pages = $products->max_num_pages;
if($products->have_posts()) : ?>

<?php 


            $pcount = count( $products->posts );
            while($products->have_posts()): $products->the_post();
            $mainphoto = get_field("main_foto");
            $get_image_id = $mainphoto[id];
            $image_link = wp_get_attachment_image_url($get_image_id, "full");
            $logo = get_field("logo_tovar");
            $get_logo_id = $logo[id];
            $logo_link = wp_get_attachment_image_url($get_logo_id, "full");             
                        ?>
                      
                      
                       <div >
         <a href="<?php the_permalink( $post ); ?>" title="<?php the_title(); ?>">
            <?php if( get_field("hot_comment") ): ?>
           <div > <span></span> <?php the_field( "hot_comment" ); ?> </div>  
        <?php endif; ?>

            <div >
               <!--<img src="<?php echo $image_link;?>" alt="" >-->
               <!--<img src="<?php echo $logo_link;?>" alt="" >-->
              <div >
                     <div  style="background-image: url('<?php echo $image_link;?>');"></div>
                  </div>
            </div>
          
            <div >
               <div >
               <p ><?php the_title(); ?></p>
               <?php if( get_field("model") ): ?>
               </div>
                  <div >
               <p ><b>Модель: </b><span ><?php the_field( "model" ); ?></span></p>
               <?php endif; ?>
               <div >
               <?php if( have_rows('harakteristiki_small') ):?>
                              
                              <div >Характеристики</div>
                                 <ul >     
                                    
                                 <?php while( have_rows('harakteristiki_small')): the_row();?>
                                 <li >
                                       <span ><?php the_sub_field('har_title');?> </span>
                                       <span ><?php the_sub_field('har_value');?></span>
                                 </li>
                                 <?php endwhile;?>                            
                                    
                                 
                                 </ul>
                                 <?php endif; ?>
               
               </div>
               <div >
                  <!--<a href="#recall" data-topic="Горячее предложение(<?php the_title(); ?>)" >Отправить запрос</a>-->
                  <ul >
                     
                    
                    <!-- <?php if( get_field("nalichie") ): ?>
                     <li >
                        Наличие:
                        <span ><?php the_field( "nalichie" ); ?></span>
                     </li>
                     <?php endif; ?>
                     <?php if( get_field("garantiya") ): ?>
                     <li >
                        Гарантия:
                        <span ><?php the_field( "garantiya" ); ?></span>
                     </li>
                     <?php endif; ?> -->
                    <?php if( get_field("cena") ): ?>
                     <li >
                        Цена:
                        <span ><?php the_field( "cena" ); ?></span>
                     </li>
                     <?php endif; ?>
                  </ul>

               </div>
            </div>
            </div>
            </a>
         </div>
                      
                         <?php
                         
                        
                        endwhile;?>


                        <?php
        if($max_pages > 1){ ?>
                       <a id="load-more-products" href="#"  data-page="2">Смотреть еще</a>
                        <?php }?>


                        <?php wp_reset_postdata();
endif;?>
                        </div>

functions.php

function get_hot_products() {
    global $post;

    
    $paged = $_POST['paged'] ? $_POST['paged'] : 1; 
    $return_html = ''; 
    $args = array( 
        'post_type' => 'product',
        'posts_per_page' => 2,
        'post_status' => 'publish',
        'meta_key'  => 'hot',
        'meta_value'    => true
    );
    
    $products = new WP_Query($args);
    $max_pages = $products->max_num_pages; 
    if ($products->have_posts()):
         
        while ($products->have_posts()): $products->the_post();
    
            $mainphoto = get_field("main_foto");
            $get_image_id = $mainphoto[id];
            $image_link = wp_get_attachment_image_url($get_image_id, "full");
            $logo = get_field("logo_tovar");
            $get_logo_id = $logo[id];
            $logo_link = wp_get_attachment_image_url($get_logo_id, "full"); 
            $title = get_the_title();
            $permalink = get_the_permalink();
            $cena = get_field("cena");
           
            
            
            $return_html .= '<div ><a href="' . $permalink . '" title="' . title . '">';
            
            if( get_field("hot_comment") ):
            $hot_comment = get_field( "hot_comment" );
            $return_html .= '<div > <span></span>' . $hot_comment . '</div>  ';
            endif;
            
            $return_html .='<div ><div ><div  style="background-image: url(' . $image_link . ');"></div></div></div>';
            
            $return_html .='<div >';
            
            $return_html .='<div ><p >' . $title . '</p></div>';
            
            $return_html .='<div >';
            
            if( get_field("model") ):
            $model = get_field( "model" );
            $return_html .= '<p ><b>Модель: </b><span >' . $model . '</span></p>';
            endif;
            
            $return_html .='<div >';
            
            if( have_rows('harakteristiki_small') ):
            $return_html .='<div >Характеристики</div><ul >';
                while( have_rows('harakteristiki_small')): the_row();
                    $har_title = get_sub_field( "har_title" );
                    $har_value = get_sub_field( "har_value" );
                    $return_html .='<li ><span >' . $har_title . '</span><span >' . $har_value . '</span></li>';
                endwhile;
            $return_html .='</ul>';
            endif;
            
            $return_html .='<div ><ul ><li >Цена:<span >' . $cena . '</span></li></ul></div>';
            
            $return_html .='</div>';
            
            $return_html .= '</div></div></a></div>';
            
            
        endwhile;
        
        
        
        

        
    endif;
    if ($paged < $max_pages)  { // if the current page is less than the total number of pages, then we display the button for loading
        $next_page = $paged   1; // in the date attributes of the button, we pass the number of the next page and the id of the current terms
        $return_html .= '<a id="load-more-products" href="#"  data-page="'. $next_page .'">Смотреть ещё</a>';
    }
    wp_reset_postdata();
    echo $return_html; // returning the html code
    wp_die(); // "die"
}


add_action('wp_ajax_get_hot_products', 'get_hot_products'); 
add_action('wp_ajax_nopriv_get_hot_products', 'get_hot_products');

CodePudding user response:

You are getting the paged value from the query string, but you're not passing it into the WP_Query args, so it's always grabbing the first ones.

    $paged = $_POST['paged'] ? $_POST['paged'] : 1; 
    $return_html = ''; 
    $args = array( 
        'post_type' => 'product',
        'posts_per_page' => 2,
  --->  'paged' => $paged,
        'post_status' => 'publish',
        'meta_key'  => 'hot',
        'meta_value'    => true
    );

CodePudding user response:

I solved the problem by putting the output of 4 products instead of 2. But this is not quite the right decision. At the moment it is suitable, since I have only 8 "hot products" on the site

 'posts_per_page' => 4,
  •  Tags:  
  • Related