Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
publicaciones duplicadas con ajax carga más wordpress

Tengo ajax cargando publicaciones en mi sitio cuando me desplazo. El problema radica en la aparición de publicaciones duplicadas. No puedo entender por qué sucede esto. Solo algunas publicaciones tienen duplicados. Aquí está mi código en function.php :

 function loadmore_get_posts(){ $paged = !empty($_POST['paged']) ? $_POST['paged'] : 1; $paged++; $args = array( 'paged' => $paged, 'posts_per_page' => $_POST['posts_per_page'], 'post_type' => 'post', 'post_status' => 'publish', 'cat' => $_POST['cats'] ); query_posts($args); while( have_posts() ) : the_post(); get_template_part( 'this is template' ); endwhile; die; } add_action('wp_ajax_loadmore', 'loadmore_get_posts'); add_action('wp_ajax_nopriv_loadmore', 'loadmore_get_posts');

Y esto es ajax en vainilla javascript:

 let ajaxurl = '<?php echo admin_url('admin-ajax.php') ?>'; let section_posts = 1; let postData = new FormData(); postData.append('action', 'loadmore'); postData.append('paged', section_posts); postData.append('posts_per_page', 9); postData.append('cats', <?php print json_encode(get_selected_cats())?>); const xhr = new XMLHttpRequest(); xhr.open('POST', ajaxurl); xhr.addEventListener('readystatechange', function (data) { if (this.readyState === 4 && this.status === 200) { section_posts++; document.querySelector('.articlefeed_template').innerHTML += data.target.responseText; } else {} xhr.send(postData); }

Por favor, dime en qué me equivoco. ¡Gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Sugeriría NO usar query_posts y usar wp_query en su lugar.

 function loadmore_get_posts(){ $paged = ! empty( $_POST['paged'] ) ? $_POST['paged'] : 1; $paged++; $args = array( 'paged' => $paged, 'posts_per_page' => $_POST['posts_per_page'], 'post_type' => 'post', 'post_status' => 'publish', 'cat' => $_POST['cats'] ); $data = new WP_Query( $args ); $count_posts = $data->found_posts; while( $data->have_posts() ) : $data->the_post(); get_template_part( 'this is template' ); endwhile; die; } add_action( 'wp_ajax_loadmore', 'loadmore_get_posts' ); add_action( 'wp_ajax_nopriv_loadmore', 'loadmore_get_posts' );

Aquí también requerirá el recuento total de publicaciones, para ocultar el botón cargar más, cuando las publicaciones hayan terminado. Verifique el código anterior, lo he agregado en la variable $count_posts .

¿Además no has pasado el offset?

En su archivo JS, pase los parámetros a continuación también

 var total_post = '<?php echo $count_posts; ?>'; postData.append('offset', section_posts*9); postData.append('totalPost', total_post);

Lea más sobre compensación aquí

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!