Advertisement
Guest User

Wordpess Custom Query and pagination attempt

a guest
Jan 4th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The template for displaying Category Archive pages.
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Twenty_Eleven
  7.  * @since Twenty Eleven 1.0
  8.  */
  9.  
  10. get_header(); ?>
  11.  
  12.         <section id="primary">
  13.             <div id="cat-content" role="main">
  14.  
  15. <?php
  16. // Create a new filtering function that will add our where clause to the query
  17. // Retrieve consistent random set of posts with pagination
  18. function mam_posts_query($query) {
  19.     global $mam_posts_query;
  20.     if ($mam_posts_query && strpos($query, 'ORDER BY RAND()') !== false) {
  21.        $query = str_replace('ORDER BY RAND()',$mam_posts_query,$query);
  22.     }
  23.     return $query;
  24. }
  25. add_filter('query','mam_posts_query');
  26.  
  27. function filter_where( $where = '' ) {
  28.     // posts in the last 120 days
  29.     $where .= " AND post_date > '" . date('Y-m-d', strtotime('-120 days')) . "'";
  30.     return $where;
  31. }
  32.  
  33. $seed = date('Ymdh');
  34. global $mam_posts_query;
  35.  
  36. add_filter( 'posts_where', 'filter_where' );
  37. $mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
  38.  
  39. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  40.  
  41. $totalpostcount = count(query_posts(array('posts_per_page'=>-1,'cat'=>6) ) );
  42. $offset = ($paged-1) * 9;
  43. query_posts( array('paged'=> $paged ,'posts_per_page'=>9,'cat'=>6,'orderby'=>'rand','offset' => $offset, ) );
  44. $wp_query->found_posts=$totalpostcount;
  45. $wp_query->max_num_pages = ceil($totalpostcount / 9);
  46.  
  47. $mam_posts_query = ''; // Turn off filter
  48. remove_filter( 'posts_where', 'filter_where' );
  49.  
  50. $counter=1;
  51. if ( have_posts() ) :        
  52.    while ( have_posts() ) : the_post();
  53.       if($counter==1||$counter==4||$counter==8) echo '<ul class="cat-col-' . $counter . '">';
  54.  
  55.         if($counter==1||$counter==9) echo '<li class=type-3>';
  56.         if($counter==2||$counter==3||$counter==4||$counter==5) echo '<li class=type-1>';
  57.         if($counter==6||$counter==7||$counter==8) echo '<li class=type-2>';
  58.  
  59.         echo '<a href="' . get_permalink( ) . '"><span class="lower"></span><span class="upper"><strong>' . get_the_title() . '</strong><em>View image »</em></span>';
  60.         if($counter==1||$counter==9) the_post_thumbnail('cat-large-square', array('class' => 'cat-large-square'));
  61.         if($counter==2||$counter==3||$counter==4||$counter==5) the_post_thumbnail('cat-small-square', array('class' => 'cat-small-square'));
  62.         if($counter==6||$counter==7||$counter==8) the_post_thumbnail('cat-rect', array('class' => 'cat-rect'));
  63.  
  64.  
  65.  
  66.       echo '</a></li>';
  67.       if($counter==3||$counter==7||$counter==9) echo '</ul>';
  68.       $counter++;
  69.    endwhile;?>
  70. <div class="nav-previous"><?php previous_posts_link( __( '<span class="meta-nav">&larr;</span>Go Back', 'twentyeleven' ) ); ?>
  71. </div>
  72.                                     <div class="nav-next"><?php next_posts_link( __( 'More in this Gallery<span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
  73.  
  74.  
  75.                 <?php /* twentyeleven_content_nav( 'nav-below' ); */ ?>
  76.  
  77.             <?php else : ?>
  78.  
  79.                 <article id="post-0" class="post no-results not-found">
  80.                     <header class="entry-header">
  81.                         <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
  82.                     </header><!-- .entry-header -->
  83.  
  84.                     <div class="entry-content">
  85.                         <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
  86.                         <?php get_search_form(); ?>
  87.                     </div><!-- .entry-content -->
  88.                 </article><!-- #post-0 -->
  89.  
  90.             <?php endif; ?>
  91.  
  92.             </div><!-- #content -->
  93.         </section><!-- #primary -->
  94.  
  95. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement