I’m working on a WordPress site and I want to AJAXify the pages to improve user experience by dynamically loading content without full page reloads. My website is TopFollow APK Guru.
What I Want to Achieve:
I would like to load different sections of my site, such as blog posts and product pages, using AJAX when users navigate the site. This should minimize page load times and make the navigation smoother.
What I’ve Tried:
- I’ve installed and activated the necessary jQuery scripts.
- Added basic AJAX functionality in my theme’s JavaScript file.
- Created a custom WordPress template to handle AJAX requests.
Issue:
Despite following several tutorials, I’m encountering issues where the content is not being loaded correctly. Sometimes the AJAX request returns the wrong data, and other times the content does not update dynamically on the page.
Code:
Here’s a simplified version of the JavaScript code I’m using:
jQuery(document).ready(function($) {
$('a.ajax-link').click(function(e) {
e.preventDefault();
var href = $(this).attr('href');
$.ajax({
url: href,
type: 'GET',
success: function(data) {
$('#content').html($(data).find('#content').html());
history.pushState(null, null, href);
},
error: function() {
alert('Failed to load content.');
}
});
});
});