I'm trying to add one feature to the included pagination script that will allow inclusion of a view all button, something like this: <prev 1 2 3 next> View All>
View all would reload the page but without pagination allowing all posts to show.
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('Newest', 'rhinoplasty'); ?></span></a>
<?php
}
// first page is not visible...
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
}
}
// last page is not visible
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('Oldest', 'rhinoplasty'); ?> ›</span></a>
<?php } ?>
<a href="">View All</a>
</div>
<?php }
Any help would be greatly appreciated, last minute addition to the project!
Christianto answers:
Hi Mike,
Is this function will be apply on blog page?
If its true, please try this, add code below, in your functions.php
if (isset($_GET['viewall']))
{
function view_allposts( $query ) {
if ( $query->is_home ) {
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'view_allposts' );
}
and change 'View all' link on your numeric pagination (<em><a href="">View All</a></em>) function to this
<?php if(!$_GET['viewall']){ ?>
<a href="<?php echo add_query_arg( array( 'viewall' => "true" ), get_pagenum_link(1) ); ?>">show all</a>
<?php } ?>
If you need an adjustment, let me know it, I will change the code..
Mike Sewell comments:
Hi Christiano,
Thanks very much for your assistance.
It's for custom post type actually.
The pagination will show on the taxonomy.php
I entered your code into functions.php, it reloaded the page with ?viewall=true
following the url, but pagination still loaded.
here is the code of the taxonomy page if this helps:
<?php get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div class="page-top-image"></div>
<div id="container">
<div id="content" role="main">
<h1 class="page-title"><?php
printf( __( ' %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></h1>
<?php query_posts($query_string . '&meta_key=item-number&orderby=meta_value&order=ASC'); ?>
<?php include (TEMPLATEPATH . '/functions/inventory-tables.php'); ?>
<?php numeric_pagination(); ?>
</div><!-- #content -->
</div><!-- #container -->
<div id="primary" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'tertiary-widget-area' ); ?>
</ul>
</div><!-- #secondary .widget-area -->
<?php get_footer(); ?>
Christianto comments:
Yes, it will reloaded with parameter "viewall=true" and it will trigger the custom code that place in functions.php to set the query and load all post without pagination..
What happen if I remove <em>$query->is_home</em> so my code above that put in functions.php will become
if (isset($_GET['viewall']))
{
function view_allposts( $query ) {
$query->set( 'posts_per_page', -1 );
}
add_action( 'pre_get_posts', 'view_allposts' );
}
Mike Sewell comments:
Hi Christiano,
Sorry that still didn't work, reloads the page but pagination is still there. I think $paged needs to be modified but I'm not sure how to do it.
Christianto comments:
Hi Mike,
Sorry I answered your question at 4am so I have to rest for a while..
As stated by Andrzej Zglobica, you can put the query mods on taxonomy.php so using Andrzej code become...
<?php get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div class="page-top-image"></div>
<div id="container">
<div id="content" role="main">
<h1 class="page-title"><?php
printf( __( ' %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></h1>
<?php
$query_postcount = ( (bool) $_GET['viewall'] ) ? '&posts_per_page=-1' : '';
query_posts($query_string . '&meta_key=item-number&orderby=meta_value&order=ASC' . $query_postcount);
?>
<?php include (TEMPLATEPATH . '/functions/inventory-tables.php'); ?>
<?php numeric_pagination(); ?>
</div><!-- #content -->
</div><!-- #container -->
<div id="primary" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'tertiary-widget-area' ); ?>
</ul>
</div><!-- #secondary .widget-area -->
<?php get_footer(); ?>
you can delete my code on functions.php..
Andrzej answers:
Hi guys,
I'm thinking Christiano's answer should work as long as you work with normal posts not a CPT but I might be wrong.
See if modifing query_posts in your template would work:
Replace:
<?php query_posts($query_string . '&meta_key=item-number&orderby=meta_value&order=ASC');
With:
<?php
$query_postcount = ( (bool) $_GET['viewall'] ) ? '&posts_per_page=-1' : '';
query_posts($query_string . '&meta_key=item-number&orderby=meta_value&order=ASC' . $query_postcount);
Mike Sewell comments:
Thank you both!!
Christianto's original code plus Andrzej's modification to the query solved the mystery. Very much appreciated guys!!