• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

How To: AJAX Post Pagination in MooTools ...

Ever wanted to browse through the older post archives only to be staring at the screen for ages in frustration while the content slowly loads up? A quick fix would be to make use of AJAX to load the post archives. In this tutorial, I will show you how to do that using the ever popular JavaScript framework; MooTools on a typical 2 column WordPress theme.

The code is quite easily digestible and with a little CSS tweaking, you could get it to work for your theme.


Step 1: Readying the Files

Download the file mootools_core.js and upload it into a folder within the directory of your active WordPress theme and name it as js.

I only included components needed for AJAX post pagination in this MooTools build. You are however welcome to create a new one with components you would like to use.

Create a blank JavaScript file called ajax.js and upload into the js folder. At this point of time, you should have 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 have to tell the browser that we have a JavaScript file to use. Add the following lines into your header.php just before the closing </head> tag.

<script src="<?php bloginfo( emplate_directory); ?>/js/mootools_core.js" type="text/javascript"></script>
<script src="<?php bloginfo( emplate_directory); ?>/js/ajax.js" type="text/javascript"></script>

Step 3: Edit index.php

When it comes to AJAX pagination, we only want to refresh the post listings and not other portions of your site. The following code snippets will tell the browser to skip loading the entire page.

The top of your index.php file should have the following line:

<?php get_header(); ?>

Replace that line with this:

<?php if (!isset($_GET[ajax])) { ?>
<?php get_header(); ?>
<?php } ?>

Similarly, at the very end of the file you should see:

<?php get_sidebar(); ?> 
<?php get_footer(); ?>

Now replace it with this: 

<?php if (!isset($_GET[ajax])) { ?>
<?php get_sidebar(); ?> 
<?php get_footer(); ?>     
<?php } ?>

To avoid any possible causes for the code not to work, do ensure that your HTML structure is identical to the ones listed below. We will need to have a div with an id of post nesting the loop.

Go back into index.php and add the div before the start of the loop, i.e. the end result should look like this:

<div id="post"> 
<?php if (have_posts()) { ?>

Be sure to also close the <div> after the loop ends.

<?php } ?>
</div>

We also need a div with a CSS class of page-navi to nest the post pagination links.

<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 basically what goes on in your JavaScript file. I have left some pretty detailed comments so that you know what the code is all about.

//function used to handle AJAX post pagination
function ajaxLinks(id, container) {
 
 //looks for all instances of id
 $$(id).each(function(ele) {  
 
  //what happens when the particular instance is clicked on
  ele.addEvent(click, function(e) {
   e = new Event(e).stop();
   var alink = ele.getProperty(href);
   var url = alink;
 
   //construct the new URL with a parameter indicating how it should load the page (fully or a portion of it)
   if (alink.indexOf(?) != -1) {
    url += "&ajax=y";
   } else {
    url += "?ajax=y";
   }    
 
   //this is where the magic happens
   var ajaxLink = new Request.HTML({    
    onRequest: function() {}, //what happens during an ajax request is made             
    onSuccess: function() { //what happens when an ajax request is completed successfully               
     new Fx.Scroll(document.body, { woulduration: long}).start(0, 0); //scrolls to the top of the page once your content is loaded
     ajaxLinks(.page-navi a, post); //calls the function again so that it will add ajax post pagination to your newly loaded page
    },
 
    onFailure: function() {}, //what happens when an ajax request fails
    update: $(container) //#post which is your container gets updated
   }).get(url);           
  });     
 
 });
 
}
 
//needed for the MooTools build to be executed
window.addEvent( wouldomready, function(dom){      
 ajaxLinks(.page-navi a, post); //Of course all of this will not be completed until you call the function to action
});

Step 5: Customization

As you would have probably guessed, this is a very basic implementation of how AJAX post pagination works in WordPress themes.

I would love to let your imagination run wild and see what you come up. Of course you need to have a little knowledge in CSS to get it done. Customizations can be made in the 3 different events (onRequest, onSuccess and onFailure).

 Original Source:
http://www.problogdesign.com/wordpress/how-to-ajax-post-pagination-in-mootools/

AddThis Social Bookmark Button

Posted at 07:12:38 am | Permalink | Posted in Tutorials  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

e-messenger

MSN Web Messenger

eBuddy

ASP.NET Ajax CalendarExtender and Validation

AIM Express

Ajax Tools for ASP.NET Developers



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (176)
  • Ajax (112)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (84)
  • Email (23)
  • Facebook (84)
  • Flash (20)
  • Google (54)
  • Html (29)
  • Image (12)
  • International Calls & VOIP (7)
  • Java (58)
  • Javascript (280)
  • jQuery (200)
  • JSON (75)
  • Perl (2)
  • PHP (172)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (32)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (227)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (29)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange