Quantcast
Channel: Lightningspirit's Blog
Viewing all articles
Browse latest Browse all 9

How to paginate a content

$
0
0

There are several ways on how to paginate a content in WordPress. There are some plugins out there, but WordPress core uses paginate_links() and his use would be more encouraged.

The following code are two functions. The first one returns boolean when tests if current $wp_query object is paginated. The second shows paginate links itself:

if ( ! function_exists( '_s_paginated' ) ):
/**
* Evaluate if actual query is paginated
*
* @since 1.1
*
* @return bool
*/
function _s_paginated() {
global $wp_query;
return ( $wp_query->max_num_pages > 1 );
}
endif; // _s_paginated
/**
 * Displays pagination for specified wp_query object
 *
 * @param object $obj WP_Query object
 * @param int|string $paged The actual current page
 * @param array $args paginate_links attributes
 *
 * @return string The pagination HTML
 */
function _s_pagination( $obj = null, $args = null ) {
 extract( wp_parse_args( $args,
 array(
 'echo' => true,
 'display' => 'html', // plain
 'id' => 'pagination',
 'class' => 'posts-navigation alignright'
 )
 )
 );
 // Check if object or main loop
 if ( is_object( $obj ) && 'wp_query' == strtolower( get_class( $obj ) ) ) {
 $total = $obj->max_num_pages;
 if ( isset( $obj->query_vars['paged'] ) && $obj->query_vars['paged'] > 1 )
 $paged = $obj->query_vars['paged'];
 // Use the main loop
 } else {
 global $wp_query;
 $total = $wp_query->max_num_pages;
 if ( isset( $obj->query_vars['paged'] ) && $obj->query_vars['paged'] > 1 )
 $paged = $obj->query_vars['paged'];
 }
 // If no page
 if ( ! isset( $paged ) )
 $paged = 1;
 // Pagination HTML generation
 $pagination = paginate_links(
 array(
 'base' => @add_query_arg( 'page', '%#%' ),
 'format' => '',
 'total' => (int) $total,
 'current' => max( 1, (int) $paged ),
 'end_size' => 0,
 'mid_size' => 1,
 )
 );
 // Switch the display method given
 if ( 'html' == $display ) : ?></pre>
<div id="<?<span class=">php echo esc_attr( $id ); ?>" role="navigation" class="<!--?php echo esc_attr( $class ); ?-->"></div>
<pre>
 <!--?<span class="hiddenSpellError" pre=""-->php echo apply_filters( '_s_pagination', $pagination ); ?>
 </div>
 <!--?<span class="hiddenSpellError" pre=""-->php
 else :
 return apply_filters( '_s_pagination', $pagination );
 endif;
}

Use it on your own projects!


Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles