Advertisement
Guest User

max_num_pages problem

a guest
Feb 28th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. ///////////////////////// Plugin PHP
  2. <?php
  3. /**
  4. * Plugin Name: PBD AJAX Load Posts
  5. * Plugin URI: http://www.problogdesign.com/
  6. * Description: Load the next page of posts with AJAX.
  7. * Version: 0.1
  8. * Author: Pro Blog Design
  9. * Author URI: http://www.problogdesign.com/
  10. */
  11.  
  12. /**
  13. * Initialization. Add our script if needed on this page.
  14. */
  15. function pbd_alp_init() {
  16. global $wp_query;
  17.  
  18. // Add code to index pages.
  19. if( !is_singular() ) {
  20. // Queue JS
  21. wp_enqueue_script(
  22. 'pbd-alp-load-posts',
  23. plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
  24. array('jquery'),
  25. '1.0',
  26. true
  27. );
  28.  
  29.  
  30.  
  31. // What page are we on? And what is the pages limit?
  32. $max = $wp_query->max_num_pages;
  33. $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
  34.  
  35. // Add some parameters for the JS.
  36. wp_localize_script(
  37. 'pbd-alp-load-posts',
  38. 'pbd_alp',
  39. array(
  40. 'startPage' => $paged,
  41. 'maxPages' => $max,
  42. 'nextLink' => next_posts($max, false)
  43. )
  44. );
  45. }
  46. }
  47. add_action('template_redirect', 'pbd_alp_init');
  48.  
  49. ?>
  50.  
  51. /////////////// Plugin JS
  52. jQuery(document).ready(function($) {
  53.  
  54. // The number of the next page to load (/page/x/).
  55. var pageNum = parseInt(pbd_alp.startPage) + 1;
  56.  
  57. // The maximum number of pages the current query can return.
  58. var max = parseInt(pbd_alp.maxPages);
  59.  
  60. // The link of the next page of posts.
  61. var nextLink = pbd_alp.nextLink;
  62.  
  63. // To check to see what the max is outputing
  64. console.log(max);
  65. /**
  66. * Replace the traditional navigation with our own,
  67. * but only if there is at least one page of new posts to load.
  68. */
  69. if(pageNum <= max) {
  70. // Insert the "More Posts" link.
  71. $('#content')
  72. .append('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
  73. .append('<p id="pbd-alp-load-posts"><a href="#">Load More Posts</a></p>');
  74.  
  75. // Remove the traditional navigation.
  76. $('.pagination').remove();
  77. }
  78.  
  79.  
  80. /**
  81. * Load new posts when the link is clicked.
  82. */
  83. $('#pbd-alp-load-posts a').click(function() {
  84.  
  85. // Are there more posts to load?
  86. if(pageNum <= max) {
  87.  
  88. // Show that we're working.
  89. $(this).text('Loading posts...');
  90.  
  91. $('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .gallery-holder',
  92. function() {
  93. $(this).hide().fadeIn(800);
  94. // Update page number and nextLink.
  95. pageNum++;
  96. nextLink = nextLink.replace(/\/page\/[0-9]*/, '/page/'+ pageNum);
  97.  
  98. // Add a new placeholder, for when user clicks again.
  99. $('#pbd-alp-load-posts')
  100. .before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
  101.  
  102. // Update the button message.
  103. if(pageNum <= max) {
  104. $('#pbd-alp-load-posts a').text('Load More Posts');
  105. } else {
  106. $('#pbd-alp-load-posts a').text('No more posts to load.');
  107. }
  108. }
  109. );
  110. } else {
  111. $('#pbd-alp-load-posts a').append('.');
  112. }
  113.  
  114. return false;
  115. });
  116. });
  117.  
  118. ///////////// The category template
  119. <?php get_header(); ?>
  120.  
  121. <section>
  122.  
  123. <hgroup>
  124. <h1><?php single_cat_title() ?></h1>
  125.  
  126. </hgroup>
  127. <ul class="sub-cat">
  128. <?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='); ?>
  129. </ul>
  130.  
  131. <?php
  132. echo '<div class="gallery" id="content">';
  133. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  134. $wp_query = new WP_Query();
  135. if(is_mobile()) {
  136. $wp_query->query('cat='.$cat.'&post_status=publish&posts_per_page=10&showpost=10&paged=' . $paged);
  137. } else {
  138. $wp_query->query('cat='.$cat.'&post_status=publish&posts_per_page=12&showpost=12&paged=' . $paged);
  139. }
  140. if ($wp_query->have_posts()) :
  141. while ($wp_query->have_posts()) : $wp_query->the_post();
  142. echo '<div class="gallery-holder">';
  143. echo '<a href="';
  144. echo the_permalink();
  145. echo '">';
  146. if(is_mobile()) {
  147. the_post_thumbnail('gallery-mobile');
  148. } else {
  149. the_post_thumbnail('gallery');
  150. }
  151. echo '<div class="desc-container"><div class="photo-desc">';
  152. echo '<h2>';
  153. the_title();
  154. echo '</h2>';
  155. the_excerpt();
  156. echo '</div></div></a></div>';
  157. endwhile;
  158. endif;
  159. echo '</div>';
  160.  
  161. if (function_exists("pagination")) {
  162. pagination($additional_loop->max_num_pages);
  163. }
  164.  
  165. ?>
  166.  
  167.  
  168. </section>
  169. <?php get_footer(); ?>
  170.  
  171. //////////////// Functions file
  172. <?php
  173. add_theme_support('post-thumbnails');
  174.  
  175. add_image_size( 'gallery', 281, 260, true );
  176. add_image_size( 'main', 984, 800, false );
  177. add_image_size( 'home', 984, 400, true );
  178. add_image_size( 'recent', 100, 100, true );
  179.  
  180. add_image_size( 'gallery-mobile', 300, 200, true );
  181. add_image_size( 'main-mobile', 300, 0, false );
  182. add_image_size( 'home-mobile', 300, 200, true );
  183. add_image_size( 'recent-mobile', 150, 150, true );
  184.  
  185. add_theme_support( 'menus' );
  186.  
  187. function custom_excerpt_length( $length ) {
  188. return 20;
  189. }
  190.  
  191. add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
  192.  
  193. function get_cat_slug($cat_id) {
  194. $cat_id = (int) $cat_id;
  195. $category = $get_category($cat_id);
  196. return $category->slug;
  197. }
  198.  
  199. $new_defaults = array(
  200. 'title_reply' => '<h4>Send</h4>',
  201. 'comment_notes_after' => '',
  202. '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>',
  203. 'title_reply' => '',
  204. 'comment_notes_before' => '',
  205. );
  206.  
  207.  
  208. function pagination($pages = '', $range = 2) {
  209. $showitems = ($range * 2)+1;
  210.  
  211. global $paged;
  212. if(empty($paged)) $paged = 1;
  213.  
  214. if($pages == '')
  215. {
  216. global $wp_query;
  217. $pages = $wp_query->max_num_pages;
  218. if(!$pages)
  219. {
  220. $pages = 1;
  221. }
  222. }
  223.  
  224. if(1 != $pages)
  225. {
  226. echo "<div class=\"pagination\">";
  227.  
  228. if(is_mobile()) {
  229. if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
  230. if($paged > 1) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
  231. } else {
  232. if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
  233. if($paged > 1) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
  234. }
  235.  
  236. for ($i=1; $i <= $pages; $i++)
  237. {
  238. if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
  239. {
  240. echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
  241. }
  242. }
  243.  
  244. if(is_mobile()) {
  245. if ($paged < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">&rsaquo;</a>";
  246. if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
  247. echo "</div>\n";
  248. } else {
  249. if ($paged < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
  250. if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
  251. echo "</div>\n";
  252. }
  253.  
  254. }
  255. }
  256. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement