Download Wordpress Messages via Ajax

So, I have this <div> which contains my blog posts. His class is .post . I want every time someone clicks on the latest posts in the widget area, the content is loaded via Ajax instead of refreshing the page.

Here is my code

 jQuery(function() { jQuery(".umrp-list li a").click(function(){ //here I select the links of the widget var post_url = jQuery(this).attr("href"); jQuery(".post").html('<div class="loading">Loading Content...</div>'); jQuery(".post").load(post_url); return false; }); }); 

So, I have two problems

  • Whenever I click the Recent Posts link, the content is loaded where I want to download it, but my webpage is inactive / frozen for about 2 seconds after clicking. Why is this happening?

  • When I click for the first time, everything works except for the details described above. When I click a second time (another link or even the same one), instead of loading the message I clicked, it loads my entire web page into this .post container. Any ideas why this is happening?

+4
source share
2 answers

So the problem was that in single.php (where the blog post template is stored), I forgot to erase <?php get_header(); ?> <?php get_header(); ?> and <?php get_footer(); ?> <?php get_footer(); ?> Now everything is fine!

0
source

It looks like you are sending the request synchronously. Synchronous operations are blocked, while asynchronous operations allow the user interface to continue working while the operation completes behind the scenes.

Regarding the second problem, it seems that jQuery (this) somehow points to the document instead of the context of the element. Check the value of "this" a second time, and it will probably give you some idea.

+1
source

Source: https://habr.com/ru/post/1482439/


All Articles