Skip to content

Instantly share code, notes, and snippets.

@mfields
Created December 6, 2010 16:16
Show Gist options
  • Save mfields/730507 to your computer and use it in GitHub Desktop.
Save mfields/730507 to your computer and use it in GitHub Desktop.
jQuery Post Loader for WordPress Home Page
<?php
/*
Plugin Name: mfields.org - Ajax Append Posts
Plugin URI:
Description:
Version: 0.1.2
Author: Michael Fields
Author URI: http://mfields.org/
Copyright 2010 Michael Fields michael@mfields.org
License GPLv2
*/
/**
* Ensure that jQuery is included on the "posts" page.
* @since 2010-12-06
*/
function mfields_ajax_append_posts_jquery() {
if ( is_home() ) {
wp_enqueue_script( 'jquery' );
}
}
add_action( 'wp_print_scripts', 'mfields_ajax_append_posts_jquery' );
/**
* Button to load more posts on the "posts" page.
*
* This action should be added to either home.php or index.php
* and should be placed outside of the div that contains posts.
*
* @param string Text to display inside the button.
* @param string Add before the button.
* @param string Add after the button.
* @return void
*
* @since 2010-12-06
*/
function mfields_ajax_append_posts_button( $text = '%s more posts.', $before = '', $after = '' ) {
if( is_home() ) {
$posts_remaining = '';
if( isset( $GLOBALS['wp_query']->found_posts ) && isset( $GLOBALS['wp_query']->post_count ) ) {
$posts_remaining = (int) $GLOBALS['wp_query']->found_posts - (int) $GLOBALS['wp_query']->post_count;
}
$text = sprintf( $text, '<span id="posts-remaining">' . $posts_remaining . '</span>' );
print $before . '<span id="load-posts">' . $text . '</span>' . $after;
}
}
add_action( 'mfields_ajax_append_posts_button', 'mfields_ajax_append_posts_button' );
/**
* Public Ajax Callback.
*
* Simulate the WordPress loop during an Ajax call.
*
* You can call this function using the following urls:
* 1. /wp-admin/admin-ajax.php?action=mfields_append_posts
* 2. /wp-admin/admin-ajax.php?action=mfields_append_posts&paged=2
*
* @return void.
*
* @since 2010-12-06
* @todo Add support for other multiple views.
*/
function mfields_ajax_append_posts_query() {
$response = array(
'html' => 'none',
'count' => 0
);
$query = array(
'post_type' => 'post',
'post_status' => 'publish'
);
if ( isset( $_GET['paged'] ) ) {
$query['paged'] = absint( $_GET['paged'] );
}
query_posts( $query );
if( have_posts() ) {
ob_start();
while( have_posts() ) {
the_post();
the_title( '<h2>', '</h2>' );
the_content();
$response['count']++;
}
$response['html'] = ob_get_contents();
ob_end_clean();
}
print json_encode( $response );
exit;
}
add_action( 'wp_ajax_mfields_append_posts', 'mfields_ajax_append_posts_query' );
add_action( 'wp_ajax_nopriv_mfields_append_posts', 'mfields_ajax_append_posts_query' );
/**
* Print javascript to the footer of the "posts" page.
*
* @return void.
*
* @todo fine-tune.
* @todo minify.
*
* @since 2010-12-06
*/
function mfields_ajax_append_posts_js() {
if ( is_home() ) {
$args = array(
'url' => admin_url( 'admin-ajax.php' ),
'total' => 0
);
if ( isset( $GLOBALS['wp_query']->found_posts ) ) {
$args['found_posts'] = absint( $GLOBALS['wp_query']->found_posts );
}
print <<< EOF
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var next_page = 2;
var total_posts = {$args['found_posts']};
var posts_in_dom = $( '#content .hentry' ).length;
var posts_remaining = total_posts - posts_in_dom;
var button = $( '#load-posts' );
var container = $( '#content' );
button.click( function () {
$.ajax( {
url : '{$args['url']}',
data : { 'action' : 'mfields_append_posts', 'paged' : next_page },
type : 'GET',
dataType : 'json',
cache : false,
success : function( response ) {
if ( 0 === response.count ) {
button.remove();
}
else {
container.append( response.html );
posts_in_dom = posts_in_dom + parseInt( response.count );
posts_remaining = total_posts - posts_in_dom;
if ( 0 === posts_remaining ) {
button.remove();
}
else {
$( '#posts-remaining' ).text( posts_remaining );
next_page++;
}
}
}
} );
} );
});
</script>
EOF;
}
}
add_action( 'wp_footer', 'mfields_ajax_append_posts_js' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment