Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Displays a page pagination if more posts are available than can be displayed on one page
- *
- * @param string|WP_Query $pages pass the number of pages instead of letting the script check the gobal paged var
- * @param string $wrapper
- * @return string returns the pagination html code
- */
- function avia_pagination($pages = '', $wrapper = 'div') //pages is either the already calculated number of pages or the wp_query object
- {
- global $paged, $wp_query;
- if(is_object($pages))
- {
- $use_query = $pages;
- $pages = '';
- }
- else
- {
- $use_query = $wp_query;
- }
- if(get_query_var('paged')) {
- $paged = get_query_var('paged');
- } elseif(get_query_var('page')) {
- $paged = get_query_var('page');
- } else {
- $paged = 1;
- }
- $output = '';
- $prev = $paged - 1;
- $next = $paged + 1;
- $range = 2; // only edit this if you want to show more page-links
- $showitems = ($range * 2)+1;
- if(is_page(array( 731, 5494, 2345))) {
- $pages = 2;
- }
- if($pages == '') //if the default pages are used
- {
- //$pages = ceil(wp_count_posts($post_type)->publish / $per_page);
- $pages = $use_query->max_num_pages;
- if(!$pages)
- {
- $pages = 1;
- }
- //factor in pagination
- if( isset($use_query->query) && !empty($use_query->query['offset']) && $pages > 1 )
- {
- $offset_origin = $use_query->query['offset'] - ($use_query->query['posts_per_page'] * ( $paged - 1 ) );
- $real_posts = $use_query->found_posts - $offset_origin;
- $pages = ceil( $real_posts / $use_query->query['posts_per_page']);
- }
- }
- $method = is_single() ? 'avia_post_pagination_link' : 'get_pagenum_link';
- /**
- * Allows to change pagination method
- *
- * @used_by avia_sc_blog 10
- * @since 4.5.6
- * @return string
- */
- $method = apply_filters( 'avf_pagination_link_method', $method, $pages, $wrapper );
- if(1 != $pages)
- {
- $output .= "<$wrapper class='pagination'>";
- $output .= "<span class='pagination-meta'>".sprintf(__("Page %d of %d", 'avia_framework'), $paged, $pages)."</span>";
- $output .= ($paged > 2 && $paged > $range+1 && $showitems < $pages)? "<a href='".$method(1)."'>«</a>":'';
- $output .= ($paged > 1 && $showitems < $pages)? "<a href='".$method($prev)."'>‹</a>":'';
- for ($i=1; $i <= $pages; $i++)
- {
- if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
- {
- switch( $i )
- {
- case ( $paged == $i ):
- $class = 'current';
- break;
- case ( ( $paged - 1 ) == $i ):
- $class = 'inactive previous_page';
- break;
- case ( ( $paged + 1 ) == $i ):
- $class = 'inactive next_page';
- break;
- default:
- $class = 'inactive';
- break;
- }
- $output .= ( $paged == $i )? "<span class='{$class}'>" . $i . "</span>" : "<a href='" . $method($i) . "' class='{$class}' >" . $i . "</a>";
- }
- }
- $output .= ($paged < $pages && $showitems < $pages) ? "<a href='".$method($next)."'>›</a>" :'';
- $output .= ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".$method($pages)."'>»</a>":'';
- $output .= "</$wrapper>\n";
- }
- return apply_filters( 'avf_pagination_output', $output, $paged, $pages, $wrapper );
- }
- /**
- *
- * @since < 4.5 - modified 4.5.5
- * @param int $page_number
- * @return string
- */
- function avia_post_pagination_link( $page_number )
- {
- global $post;
- //the _wp_link_page uses get_permalink() which might be changed by a query. we need to get the original post id temporarily
- $temp_post = $post;
- // $post = get_post(avia_get_the_id());
- /**
- * With WP 5.1 returns an extra class that breaks our HTML link
- */
- $html = _wp_link_page( $page_number );
- $match = array();
- preg_match('/href=["\']?([^"\'>]+)["\']?/', $html, $match );
- $url = isset( $match[1] ) ? $match[1] : '';
- $post = $temp_post;
- /**
- * @since 4.5.5
- * @return string
- */
- return apply_filters( 'avf_pagination_post_pagination_link', $url, $page_number );
- }
Add Comment
Please, Sign In to add comment