Advertisement
jeradin

Add Page dropdown to Gravity Views

Feb 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1. /* Filter search criteria */
  2. add_filter( 'gravityview_search_criteria', function( $criteria ) {         
  3.     if( isset($_GET['custom_count']) ) {
  4.         $criteria['paging']['page_size'] = $_GET['custom_count'];    
  5.     };
  6.     return $criteria;
  7. } );
  8.  
  9. /*
  10. * Create Shortcode
  11. * Usage: [gv_custom_page_dropdown]
  12. */
  13. add_shortcode('gv_custom_page_dropdown', 'gv_custom_page_dropdown');
  14. function gv_custom_page_dropdown($atts) {
  15.  
  16.     global $gravityview_view;
  17.  
  18.     $pagination_counts = $gravityview_view->getPaginationCounts();
  19.  
  20.     $total = $pagination_counts['total'];
  21.     $last = $pagination_counts['last'];
  22.  
  23.     $minimum_posts_per_page = isset($atts['minimum-posts-per-page']) ? $atts['minimum-posts-per-page'] : 25;
  24.     if($total <= $minimum_posts_per_page) {
  25.         return;
  26.     }
  27.  
  28.     $current_posts_per_page = isset($_GET['custom_count']) ? intval($_GET['custom_count']) : $minimum_posts_per_page;
  29.  
  30.     // Begin posts_per_page dropdown
  31.     $customDropdown ='<form method="get" class="form-inline">';
  32.     $customDropdown .='<select class="btn btn-default" name="custom_count">';
  33.         // Create all custom options below total
  34.         $x = $minimum_posts_per_page;
  35.         while($x < $total) {
  36.             $customDropdown .= "<option value='$x'";
  37.             if($x == $current_posts_per_page) {
  38.                 $customDropdown .=  ' selected';
  39.             }
  40.             $customDropdown .= ">$x</option>";
  41.             $x *= 2;
  42.         }
  43.         // Create final option
  44.         $customDropdown .= "<option value='$x'";
  45.         if($x == $current_posts_per_page) {
  46.             $customDropdown .=  ' selected';
  47.         }
  48.         $customDropdown .= ">$x</option>";
  49.  
  50.     $customDropdown .= '</select>';
  51.  
  52.     $customDropdown .='<input type="submit" class="btn btn-xs btn-primary" value="Update" />';
  53.     $customDropdown .='</form>';
  54.  
  55.     return $customDropdown;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement