Created
December 6, 2010 16:16
-
-
Save mfields/730507 to your computer and use it in GitHub Desktop.
Revisions
-
mfields revised this gist
Dec 7, 2010 . 1 changed file with 49 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ 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 @@ -12,7 +12,7 @@ /** * Ensure that jQuery is included on the "posts" page. * @since 2010-12-06 */ function mfields_ajax_append_posts_jquery() { @@ -24,45 +24,69 @@ function 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; } @@ -71,24 +95,23 @@ function 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"> @@ -109,12 +132,12 @@ function mfields_ajax_append_posts_js() { 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(); -
mfields revised this gist
Dec 6, 2010 . 1 changed file with 38 additions and 41 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,8 @@ /** * Ensure that jQuery is included on the homepage. * @since 2010-12-06 */ function mfields_ajax_append_posts_jquery() { if ( is_home() ) { wp_enqueue_script( 'jquery' ); @@ -22,11 +23,15 @@ function mfields_ajax_append_posts_jquery() { add_action( 'wp_print_scripts', 'mfields_ajax_append_posts_jquery' ); /** * Button to load more posts. * This should be added outside the div that contains posts. * @since 2010-12-06 */ function mfields_ajax_append_posts_button( $format = '%d more posts.' ) { $posts_remaining = (int) $GLOBALS['wp_query']->found_posts - (int) $GLOBALS['wp_query']->post_count; $text = sprintf( $format, '<span id="posts-remaining">' . $posts_remaining . '</span>' ); print '<div id="load-posts">' . $text . '</div>'; } add_action( 'mfields_ajax_append_posts_button', 'mfields_ajax_append_posts_button' ); @@ -42,33 +47,27 @@ function mfields_ajax_append_posts_button( $format = "%d more posts." ) { * @since 2010-12-06 */ function mfields_ajax_append_posts_query() { $response = array(); query_posts( 'paged=' . $_GET['paged'] ); $response['post_count'] = (int) $GLOBALS['wp_query']->post_count; if( have_posts() ) { ob_start(); while( have_posts() ) { the_post(); the_title( '<h2>', '</h2>' ); the_content(); } $response['html'] = ob_get_contents(); ob_end_clean(); } else { $response['html'] = 'none'; } 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' ); /** @@ -84,29 +83,28 @@ function mfields_ajax_append_posts_query() { function mfields_ajax_append_posts_js() { if ( is_home() ) { global $wp_query; $args = array( 'url' => admin_url( 'admin-ajax.php' ), 'total' => 0 ); if ( isset( $wp_query->found_posts ) ) { $args['found_posts'] = (int) $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, @@ -118,18 +116,17 @@ function append_posts() { container.append( response.html ); posts_in_dom = posts_in_dom + parseInt( response.post_count ); posts_remaining = total_posts - posts_in_dom; if ( 0 === posts_remaining ) { button.remove(); } else { $( '#posts-remaining' ).text( posts_remaining ); next_page++; } } } } ); } ); }); </script> EOF; -
mfields revised this gist
Dec 6, 2010 . 1 changed file with 69 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,14 +3,17 @@ Plugin Name: mfields.org - Ajax Append Posts Plugin URI: Description: Version: 0.1.1 Author: Michael Fields Author URI: http://mfields.org/ Copyright 2010 Michael Fields michael@mfields.org License GPLv2 */ /** * Ensure that jQuery is included on the homepage. */ function mfields_ajax_append_posts_jquery() { if ( is_home() ) { wp_enqueue_script( 'jquery' ); @@ -19,24 +22,41 @@ function mfields_ajax_append_posts_jquery() { add_action( 'wp_print_scripts', 'mfields_ajax_append_posts_jquery' ); function mfields_ajax_append_posts_button( $format = "%d more posts." ) { $posts_remaining = (int) $GLOBALS['wp_query']->found_posts = $GLOBALS['wp_query']->post_count; $text = sprintf( $format, $posts_remaining ); print '<div id="load-posts">' . $text . '</div>'; pr( $GLOBALS['wp_query'] ); } add_action( 'mfields_ajax_append_posts_button', 'mfields_ajax_append_posts_button' ); /** * Ajax callback. * * @return void. * * @todo check indexes. * @todo move to admin-ajax.php??? * * @since 2010-12-06 */ function mfields_ajax_append_posts_query() { if ( ! is_admin() && isset( $_GET['action'] ) && 'mfields_load_posts' === $_GET['action'] ) { $response = array(); global $query_string; query_posts( $query_string . '&paged=' . $_GET['paged'] ); $response['post_count'] = (int) $GLOBALS['wp_query']->post_count; if( have_posts() ) { ob_start(); while( have_posts() ) { the_post(); the_title( '<h2>', '</h2>' ); the_content(); } $response['html'] = ob_get_contents(); ob_end_clean(); @@ -47,27 +67,46 @@ function mfields_ajax_append_posts_query() { print json_encode( $response ); exit; } } add_action( 'init', 'mfields_ajax_append_posts_query' ); /** * Print javascript to the footer. * * @return void. * * @todo fine-tune javascript. * @todo minify javascript. * * @since 2010-12-06 */ function mfields_ajax_append_posts_js() { if ( is_home() ) { global $wp_query; $found_posts = 0; if ( isset( $wp_query->found_posts ) ) { $found_posts = $wp_query->found_posts; } print <<< EOF <script type="text/javascript"> jQuery( document ).ready( function( $ ) { var next_page = 2; var total_posts = {$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 () { append_posts(); } ); function append_posts() { $.ajax( { url : document.location.href, data : { 'action' : 'mfields_load_posts', 'paged' : next_page }, type : 'GET', dataType : 'json', cache : false, @@ -77,13 +116,20 @@ function mfields_ajax_get_next_posts() { } else { container.append( response.html ); posts_in_dom = posts_in_dom + parseInt( response.post_count ); posts_remaining = total_posts - posts_in_dom; if ( 0 === posts_remaining ) { button.remove(); } else { button.append( posts_remaining ); next_page++; } } } } ); }; }); </script> EOF; -
mfields created this gist
Dec 6, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ <?php /* Plugin Name: mfields.org - Ajax Append Posts Plugin URI: Description: Version: 0.1 Author: Michael Fields Author URI: http://mfields.org/ Copyright 2010 Michael Fields michael@mfields.org License GPLv2 */ function mfields_ajax_append_posts_jquery() { if ( is_home() ) { wp_enqueue_script( 'jquery' ); } } add_action( 'wp_print_scripts', 'mfields_ajax_append_posts_jquery' ); function mfields_ajax_append_posts_query() { if( is_admin() ) { return false; } $response = array(); if ( isset( $_GET['action'] ) && 'mfields_load_posts' === $_GET['action'] ) { global $query_string; query_posts( $query_string . '&paged=' . $_GET['paged'] ); global $wp_query; $response['post_count'] = (int) $wp_query->post_count; if( have_posts() ) { ob_start(); while( have_posts() ) { the_post(); get_template_part( 'post', 'switchboard' ); } $response['html'] = ob_get_contents(); ob_end_clean(); } else { $response['html'] = 'none'; } print json_encode( $response ); exit; } return; } add_action( 'init', 'mfields_ajax_append_posts_query' ); function mfields_ajax_append_posts_js() { if ( is_home() ) { print <<< EOF <script type="text/javascript"> jQuery( document ).ready( function( $ ) { var page = 1; var fetching = false; var button = $( '#mfields-load-posts' ); var container = $( '#content' ); function mfields_ajax_get_next_posts() { page++; fetching = true; $.ajax( { url : document.location.href, data : { 'action' : 'mfields_load_posts', 'paged' : page }, type : 'GET', dataType : 'json', cache : false, success : function( response ) { if ( 0 === response.post_count ) { button.remove(); } else { container.append( response.html ); } } } ); }; button.click( function () { mfields_ajax_get_next_posts(); } ); }); </script> EOF; } } add_action( 'wp_footer', 'mfields_ajax_append_posts_js' );