Advertisement
ansi

Add pagination template vars to TBTestimonials WP Plugin

Jul 5th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. class TBTestimonialPaginate
  2. {
  3.         public $per_page;
  4.  
  5.         /**
  6.         * magic
  7.         *
  8.         * @param mixed $per_page - testimonials to show per page
  9.         * @return TBTestimonialPaginate
  10.         */
  11.         public function __construct( $per_page )
  12.         {
  13.             add_action( 'tbt_template_functions', array( &$this, 'add_vars' ) );
  14.             $this->per_page = intval( $per_page );
  15.         }
  16.  
  17.         /**
  18.         * add pagination vars to testimonial templates
  19.         *
  20.         * @param mixed $twig
  21.         */
  22.         public function add_vars( $twig )
  23.         {
  24.             $twig->addGlobal( 'paged', call_user_func( 'TBTestimonialPaginate::get_testimonial_page' ) );
  25.             $twig->addGlobal( 'testimonials', call_user_func( 'TBTestimonialPaginate::get_testimonials' ) );
  26.             $twig->addGlobal( 'next_testimonial_link', call_user_func( 'TBTestimonialPaginate::get_next_testimonial_link' ) );
  27.             $twig->addGlobal( 'prev_testimonial_link', call_user_func( 'TBTestimonialPaginate::get_prev_testimonial_link' ) );
  28.         }
  29.  
  30.         /**
  31.         * get current page testimonials page
  32.         *
  33.         */
  34.         public static function get_testimonial_page(){
  35.             return isset( $_GET['paged_testimonials'] ) ? intval( $_GET['paged_testimonials'] ) : 1;
  36.         }
  37.  
  38.         /**
  39.         * get testimonials for a specific page
  40.         *
  41.         */
  42.         public static function get_testimonials()
  43.         {
  44.             global $tbtpaginate;
  45.             $q = new wp_query( array(
  46.                 'posts_per_page' => $tbtpaginate->per_page,
  47.                 'post_type' => 'testimonial',
  48.                 'paged' => self::get_testimonial_page(),
  49.                 'post_status' => 'publish'
  50.             ) );
  51.  
  52.             if( $q->have_posts() )
  53.                 return $q->posts;
  54.  
  55.             return array();
  56.         }
  57.  
  58.         /**
  59.         * link for next testimonial in pagination
  60.         *
  61.         */
  62.         public static function get_next_testimonial_link()
  63.         {
  64.             $page = self::get_testimonial_page();
  65.             $total = self::get_total_testimonial_pages();
  66.  
  67.             if( ++$page > $total )
  68.                 return false;
  69.  
  70.             return add_query_arg( array( 'paged_testimonials' => $page ) );
  71.         }
  72.  
  73.         /**
  74.         * link for previous testimonial in pagination
  75.         *
  76.         */
  77.         public static function get_prev_testimonial_link()
  78.         {
  79.             $page = self::get_testimonial_page();
  80.  
  81.             if( --$page <= 0 )
  82.                 return false;
  83.  
  84.             return add_query_arg( array( 'paged_testimonials' => $page ) );
  85.         }
  86.  
  87.         /**
  88.         * get total page count
  89.         *
  90.         */
  91.         public static function get_total_testimonial_pages()
  92.         {
  93.             global $tbtpaginate;
  94.             $q = new wp_query( array(
  95.                 'posts_per_page' => -1,
  96.                 'post_type' => 'testimonial',
  97.                 'paged' => self::get_testimonial_page(),
  98.                 'post_status' => 'publish'
  99.             ) );
  100.  
  101.             if( $q->have_posts() )
  102.                 return ceil( count( $q->posts ) / $tbtpaginate->per_page );
  103.  
  104.             return 0;
  105.         }
  106. }
  107.  
  108. $tbtpaginate = new TBTestimonialPaginate(2); # 2 testimonials per page. not variable $tbtpaginate is used in TBTestimonialPaginate::get_testimonials and in TBTestimonialPaginate::get_total_testimonial_pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement