The code below (which I use in functions) is from https://stackoverflow.com/questions/35263445/numeric-pagination-on-my-custom-category-page, and is showing a debug message of 'Undefined variable: additional_loop' and 'Trying to get property of non-object'.
function paginate ( $pages = '', $range = 3 ) {
$showitems = ( $range * 2 ) +1;
global $paged;
if ( empty ( $paged ) ) $paged = 1;
if ( $pages == '' ) {
global $wp_query;
$pages = $wp_query->max_num_pages;
if ( !$pages ) {
$pages = 1;
} }
if ( 1 != $pages ) {
echo "<p class=\"paginate\"><span>Page ".$paged." of ".$pages." </span>";
if ( $paged > 2 && $paged > $range+1 && $showitems < $pages ) echo " <a href='".get_pagenum_link ( 1 ) ."'>« First</a> ";
if ( $paged > 1 && $showitems < $pages ) echo " <a href='".get_pagenum_link ( $paged - 1 ) ."'>‹ Previous</a> ";
for ( $i=1; $i <= $pages; $i++ ) {
if ( 1 != $pages && ( ! ( $i >= $paged+$range+1 || $i <= $paged-$range-1 ) || $pages <= $showitems ) ) {
echo ( $paged == $i ) ? "<span class=\"current\"> ".$i." </span>":"<a href='".get_pagenum_link ( $i ) ."' class=\"inactive\"> ".$i." </a>";
} }
if ( $paged < $pages && $showitems < $pages ) echo " <a href=\"".get_pagenum_link ( $paged + 1 ) ."\">Next ›</a> ";
if ( $paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages ) echo " <a href='".get_pagenum_link ( $pages ) ."'>Last »</a> ";
echo "</p>\n";
} }
And...
if (function_exists( 'paginate' ) ) {
paginate( $additional_loop->max_num_pages );
}
What do I need to change to fix it?
Having been asked in a comment by @kero to include more of the code I'm using, the query (which is in the page template (along with the 'if function_exists' stuff) is below.
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query( array(
'post_type' => 'item',
'posts_per_page' => '5',
'orderby' => 'date',
'order' => 'desc',
'paged' => $paged,
) );
$additional_loop
? If your code is the same from the OP, then it should probably be$custom_query
paginate($additional_loop...)
- specifically the query. If you use the same code as the OP from the question, you'll need to use$custom_query
instead$additional_loop
variable hasn't been set with anything. I think you'd be better off replying to the Stack Overflow question/answer to get help with this.