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

197
Views
Mostrar publicación aleatoria en Wordpress y actualizar el contenido sin actualizar la página

Estoy creando un sitio web de Wordpress donde necesito mostrar una publicación aleatoria (a través de WP_Query) y volver a cargar su contenido con un clic de un botón sin actualizar toda la página.

Este es mi HTML

 <div id="randompost"></div> <button type="button" id="reloadpost">Reload post</button>

Y esta es mi WP_Query

 <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'rand', ); $arr_posts = new WP_Query( $args ); if ( $arr_posts->have_posts() ) : while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); ?> <h2><?php the_title();?></h2> <p><?php the_content();?></p> <?php endwhile; endif; ?>

He leído en línea sobre AJAX, pero no soy tan bueno en el desarrollo de JS y no pude encontrar la manera de hacerlo funcionar.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para mí, la forma más fácil es agregar la función de recarga de jQuery. Entonces, debajo de su HTML con botón, agregará

 <script> $(document).on('click', '#reloadpost', function(){ $("#randompost").load(location.href + " #randompost"); }); </script>

(También debe usar jQuery, así que si no lo tiene en su página, agregue <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> )

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!