Wp custom post with pagination

Wp custom post with pagination





$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';

$newsList = new WP_Query(array(
'post_type' => 'news',
'orderby' => 'display_order',
'order' => 'ASC',
'posts_per_page'=>  NEWS_POST_PAGINATION_LIMIT,
'paged'         => $paged,
    'nopaging' => false,
));
if($newsList->have_posts()) {

 .....

}
echo wpbeginner_numeric_posts_nav();
wp_reset_postdata();





function wpbeginner_numeric_posts_nav() {

/*if( is_singular() )
return;*/

global $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;
}

$newsQuery = new WP_Query(array(
'post_type' => 'news',
'orderby' => 'display_order',
'order' => 'ASC',
'posts_per_page'=>  NEWS_POST_PAGINATION_LIMIT,
'paged'          => $paged
));

/** Stop execution if there's only 1 page */
if( $newsQuery->max_num_pages <= 1 )
return;

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max   = intval( $newsQuery->max_num_pages );
//var_dump($paged);die;
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;

/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}

echo '<div class="navigation"><ul class="pagination_List">' . "\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li class="pagination_Item pagination_Item-previous">%s</li>' . "\n", get_previous_posts_link(" &lsaquo; ") );

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? 'active ' : '';
$class .= " pagination_Item";

$activeLinkClass = (1 == $paged ? "pagination_Link-current":"");
$activeLinkClass .=" pagination_Link";
printf( '<li class="%s"><a href="%s" class="pagination_Link '.$activeLinkClass.'">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

if ( ! in_array( 2, $links ) )
echo '<li class="pagination_Item">…</li>';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );

//var_dump($links);die;
foreach ( (array) $links as $link ) {
$class = $paged == $link ? 'active' : '';
$class .= " pagination_Item";

$activeLinkClass = ($link == $paged ? "pagination_Link-current":"");
$activeLinkClass .=" pagination_Link";
printf( '<li class="%s"><a href="%s" class="'.$activeLinkClass.'">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li class="pagination_Item">…</li>' . "\n";

$class = $paged == $max ? ' active ' : '';
$class .= " pagination_Item";

$activeLinkClass = ($max == $paged ? "pagination_Link-current":"");
$activeLinkClass .=" pagination_Link";
printf( '<li class="%s"><a href="%s" class="'.$activeLinkClass.'">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}

/** Next Post Link */
if ( get_next_posts_link("&rsaquo;",$max) )
printf( '<li class="pagination_Item pagination_Item-next">%s</li>' . "\n", get_next_posts_link("&rsaquo;",$max) );

echo '</ul></div>' . "\n";

}

Share this

Related Posts

Previous
Next Post »