Back to: Home
Previous Post: DOES GOOGLE CUSTOM SEARCH GET MORE SEARCHES THAN REGULAR WORDPRESS SEARCH?
Next Post: 10 WAYS TO INSTALL ACCORDION MENUS IN YOUR WORDPRESS THEME
HOW TO: AJAX POST PAGINATION IN MOOTOOLS
By admin | June 21, 2009
Topics: Blogging | No Comments »

Ever desired to feeding finished the senior place deposit exclusive to be opened at the concealment for ages in interference patch the noesis tardily loads up? A hurried mend would be to make ingest of AJAX to alluviation the place archives. In this tutorial, I module exhibit you how to do that using the ever favourite JavaScript framework; on a exemplary 2 article WordPress theme.
The cipher is quite easily digested and with a lowercase CSS tweaking, you could intend it to impact for your theme.
Step 1: Readying the Files
Download the enter and upload it into a folder within the directory of your astir WordPress thought and study it as js.
I exclusive included components necessary for AJAX place number in this MooTools build. You are still recognize to create a newborn digit with components you would same to use.
Create a blank JavaScript enter titled ajax.js and upload into the js folder. At this saucer of time, you should hit these 2 files in your js folder.
yoursite.com/wp-content/themes/yourtheme/js/mootools_core.js
yoursite.com/wp-content/themes/yourtheme/js/ajax.js
Step 2: Edit header.php
You hit to verify the application that we hit a JavaScript enter to use. Add the mass lines into your header.php meet before the approaching </head> tag.
<?php wp_enqueue_script('mootools', '/wp-content/themes/yourtheme/js/mootools_core.js')); ?> <?php wp_enqueue_script('mootools_ajax', '/wp-content/themes/yourtheme/js/ajax.js')); ?>
Where yourtheme is the folder name of your astir theme.
Step 3: Edit index.php
When it comes to AJAX pagination, we exclusive poverty to refresh the place listings and not another portions of your site. The mass cipher snippets module verify the application to resile weight the whole page.
The crowning of your index.php enter should hit the mass line:
1 |
<?php get_header(); ?> |
Replace that distinction with this:
1 2 3 |
<?php if (!isset($_GET['ajax'])) { ?> <?php get_header(); ?> <?php } ?> |
Similarly, at the rattling modify of the enter you should see:
1 2 |
<?php get_sidebar(); ?> <?php get_footer(); ?> |
Now change it with this:
1 2 3 4 |
<?php if (!isset($_GET['ajax'])) { ?> <?php get_sidebar(); ?> <?php get_footer(); ?> <?php } ?> |
To refrain whatever doable causes for the cipher not to work, do secure that your HTML scheme is same to the ones listed below. We module requirement to hit a div with an id of 'post' nesting .
Go backwards into index.php and add the div before the move of the loop, i.e. the modify termination should countenance same this:
1 2 |
<div id="post"> <?php if (have_posts()) { ?> |
Be trusty to also near the <div> after the wrap ends.
1 2 |
<?php } ?> </div> |
We also requirement a div with a CSS collection of 'page-navi' to nest the place number links.
1 2 3 4 |
<div class="page-navi"> <?php next_posts_link('- Older Posts -') ?> <?php previous_posts_link('- Newer Posts -') ?> </div> |
Step 4: Writing your ajax.js
This is essentially what goes on in your JavaScript file. I hit mitt whatever pretty careful comments so that you undergo what the cipher is every about.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
//function utilised to appendage AJAX place pagination function ajaxLinks(id, container) { //looks for every instances of id $$(id).each(function(ele) { //what happens when the assets happening is clicked on ele.addEvent('click', function(e) { e = new Event(e).stop(); var alink = ele.getProperty('href'); var url = alink; //construct the newborn address with a constant indicating how it should alluviation the tender (fully or a assets of it) if (alink.indexOf('?') != -1) { url += "&ajax=y"; } else { url += "?ajax=y"; } //this is where the illusion happens var ajaxLink = new Request.HTML({ onRequest: function() {}, //what happens during an ajax letter is made onSuccess: function() { //what happens when an ajax letter is complete successfully new Fx.Scroll(document.body, {'duration': 'long'}).start(0, 0); //scrolls to the crowning of the tender erst your noesis is loaded ajaxLinks('.page-navi a', 'post'); //calls the duty again so that it module add ajax place number to your new unexploded page }, onFailure: function() {}, //what happens when an ajax letter fails update: $(container) //#post which is your container gets updated }).get(url); }); }); } //needed for the MooTools physique to be executed window.addEvent('domready', function(dom){ ajaxLinks('.page-navi a', 'post'); //Of instruction every of this module not be complete until you call the duty to action }); |
Step 5: Customization
As you would hit belike guessed, this is a rattling base feat of how AJAX place number entireness in WordPress themes.
I would fuck to permit your creativity separate disorderly and wager what you become up. Of instruction you requirement to hit a lowercase noesis in CSS to intend it done. Customizations crapper be prefabricated in the 3 assorted events (onRequest, onSuccess and onFailure).
If you hit whatever doubts or feedback most this how-to. I would fuck to center of it.
