I found this custom wordpress pagination somewhere and tried using it. The pagination and the "view all" functionality works fine but the problem is with the "next" and "previous" function. When click the "next button" it skips to the last page. Can you please help me debug this code? I am not really familiar with php, it's actually my first time integrating to wordpress. Thank you!
function numeric_pagination ($pageCount = 9, $query = null) {
if ($query == null) {
global $wp_query;
$query = $wp_query;
}
if ($query->max_num_pages <= 1) {
return;
}
$pageStart = 1;
$paged = $query->query_vars['paged'];
// set current page if on the first page
if ($paged == null) {
$paged = 1;
}
// work out if page start is halfway through the current visible pages and if so move it accordingly
if ($paged > floor($pageCount / 2)) {
$pageStart = $paged - floor($pageCount / 2);
}
if ($pageStart < 1) {
$pageStart = 1;
}
// make sure page start is
if ($pageStart + $pageCount > $query->max_num_pages) {
$pageCount = $query->max_num_pages - $pageStart;
}
?>
<div id="pagination">
<?php
if ($paged != 1) {
?>
<a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>‹‹ <?php _e('Previous Page', 'rhinoplasty'); ?></span></a>
<?php
}
if ($pageStart > 1) {
//echo 'previous';
}
for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
if ($p == $paged) {
?>
<span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
<?php } else { ?>
<a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>
<?php
}
}
if ($pageStart + $pageCount < $query->max_num_pages) {
//echo "last";
}
if ($paged != $query->max_num_pages) {
?>
<a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e('Next Page', 'rhinoplasty'); ?> ››</span></a>
<?php } ?>
<?php if(!$_GET['viewall']){ ?>
<a href="<?php echo add_query_arg( array( 'viewall' => "true" ), get_pagenum_link(1) ); ?>">View All</a>
<?php } ?>
</div>
<?php
}
if (isset($_GET['viewall']))
{
function view_allposts( $query ) {
$query->set( 'posts_per_page', -1 );
}
add_action( 'pre_get_posts', 'view_allposts' );
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire