Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////// Plugin PHP
- <?php
- /**
- * Plugin Name: PBD AJAX Load Posts
- * Plugin URI: http://www.problogdesign.com/
- * Description: Load the next page of posts with AJAX.
- * Version: 0.1
- * Author: Pro Blog Design
- * Author URI: http://www.problogdesign.com/
- */
- /**
- * Initialization. Add our script if needed on this page.
- */
- function pbd_alp_init() {
- global $wp_query;
- // Add code to index pages.
- if( !is_singular() ) {
- // Queue JS
- wp_enqueue_script(
- 'pbd-alp-load-posts',
- plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
- array('jquery'),
- '1.0',
- true
- );
- // What page are we on? And what is the pages limit?
- $max = $wp_query->max_num_pages;
- $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
- // Add some parameters for the JS.
- wp_localize_script(
- 'pbd-alp-load-posts',
- 'pbd_alp',
- array(
- 'startPage' => $paged,
- 'maxPages' => $max,
- 'nextLink' => next_posts($max, false)
- )
- );
- }
- }
- add_action('template_redirect', 'pbd_alp_init');
- ?>
- /////////////// Plugin JS
- jQuery(document).ready(function($) {
- // The number of the next page to load (/page/x/).
- var pageNum = parseInt(pbd_alp.startPage) + 1;
- // The maximum number of pages the current query can return.
- var max = parseInt(pbd_alp.maxPages);
- // The link of the next page of posts.
- var nextLink = pbd_alp.nextLink;
- // To check to see what the max is outputing
- console.log(max);
- /**
- * Replace the traditional navigation with our own,
- * but only if there is at least one page of new posts to load.
- */
- if(pageNum <= max) {
- // Insert the "More Posts" link.
- $('#content')
- .append('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
- .append('<p id="pbd-alp-load-posts"><a href="#">Load More Posts</a></p>');
- // Remove the traditional navigation.
- $('.pagination').remove();
- }
- /**
- * Load new posts when the link is clicked.
- */
- $('#pbd-alp-load-posts a').click(function() {
- // Are there more posts to load?
- if(pageNum <= max) {
- // Show that we're working.
- $(this).text('Loading posts...');
- $('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .gallery-holder',
- function() {
- $(this).hide().fadeIn(800);
- // Update page number and nextLink.
- pageNum++;
- nextLink = nextLink.replace(/\/page\/[0-9]*/, '/page/'+ pageNum);
- // Add a new placeholder, for when user clicks again.
- $('#pbd-alp-load-posts')
- .before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
- // Update the button message.
- if(pageNum <= max) {
- $('#pbd-alp-load-posts a').text('Load More Posts');
- } else {
- $('#pbd-alp-load-posts a').text('No more posts to load.');
- }
- }
- );
- } else {
- $('#pbd-alp-load-posts a').append('.');
- }
- return false;
- });
- });
- ///////////// The category template
- <?php get_header(); ?>
- <section>
- <hgroup>
- <h1><?php single_cat_title() ?></h1>
- </hgroup>
- <ul class="sub-cat">
- <?php wp_list_categories('show_option_none=&orderby=name&show_count=1&hide_empty=1&use_desc_for_title=1&child_of='.$cat.'&title_li='); ?>
- </ul>
- <?php
- echo '<div class="gallery" id="content">';
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- $wp_query = new WP_Query();
- if(is_mobile()) {
- $wp_query->query('cat='.$cat.'&post_status=publish&posts_per_page=10&showpost=10&paged=' . $paged);
- } else {
- $wp_query->query('cat='.$cat.'&post_status=publish&posts_per_page=12&showpost=12&paged=' . $paged);
- }
- if ($wp_query->have_posts()) :
- while ($wp_query->have_posts()) : $wp_query->the_post();
- echo '<div class="gallery-holder">';
- echo '<a href="';
- echo the_permalink();
- echo '">';
- if(is_mobile()) {
- the_post_thumbnail('gallery-mobile');
- } else {
- the_post_thumbnail('gallery');
- }
- echo '<div class="desc-container"><div class="photo-desc">';
- echo '<h2>';
- the_title();
- echo '</h2>';
- the_excerpt();
- echo '</div></div></a></div>';
- endwhile;
- endif;
- echo '</div>';
- if (function_exists("pagination")) {
- pagination($additional_loop->max_num_pages);
- }
- ?>
- </section>
- <?php get_footer(); ?>
- //////////////// Functions file
- <?php
- add_theme_support('post-thumbnails');
- add_image_size( 'gallery', 281, 260, true );
- add_image_size( 'main', 984, 800, false );
- add_image_size( 'home', 984, 400, true );
- add_image_size( 'recent', 100, 100, true );
- add_image_size( 'gallery-mobile', 300, 200, true );
- add_image_size( 'main-mobile', 300, 0, false );
- add_image_size( 'home-mobile', 300, 200, true );
- add_image_size( 'recent-mobile', 150, 150, true );
- add_theme_support( 'menus' );
- function custom_excerpt_length( $length ) {
- return 20;
- }
- add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
- function get_cat_slug($cat_id) {
- $cat_id = (int) $cat_id;
- $category = $get_category($cat_id);
- return $category->slug;
- }
- $new_defaults = array(
- 'title_reply' => '<h4>Send</h4>',
- 'comment_notes_after' => '',
- 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
- 'title_reply' => '',
- 'comment_notes_before' => '',
- );
- function pagination($pages = '', $range = 2) {
- $showitems = ($range * 2)+1;
- global $paged;
- if(empty($paged)) $paged = 1;
- if($pages == '')
- {
- global $wp_query;
- $pages = $wp_query->max_num_pages;
- if(!$pages)
- {
- $pages = 1;
- }
- }
- if(1 != $pages)
- {
- echo "<div class=\"pagination\">";
- if(is_mobile()) {
- if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
- if($paged > 1) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
- } else {
- if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
- if($paged > 1) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
- }
- for ($i=1; $i <= $pages; $i++)
- {
- if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
- {
- echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
- }
- }
- if(is_mobile()) {
- if ($paged < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">›</a>";
- if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
- echo "</div>\n";
- } else {
- if ($paged < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
- if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
- echo "</div>\n";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement