1

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 ) ."'>&laquo; First</a> ";
if ( $paged > 1 && $showitems < $pages ) echo " <a href='".get_pagenum_link ( $paged - 1 ) ."'>&lsaquo; 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 &rsaquo;</a> ";
if ( $paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages ) echo " <a href='".get_pagenum_link ( $pages ) ."'>Last &raquo;</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,
) );
  • What is $additional_loop? If your code is the same from the OP, then it should probably be $custom_query – kero May 15 '18 at 14:55
  • @kero '$additional_loop' is from the answer to the original question... which, with the functions code also in the answer, is what I'm using - and assume (perhaps wrongly,, I'm not an expert) works regardless of the query name. – glvr May 15 '18 at 15:51
  • 2
    Please show more of the code where you're using 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 – kero May 15 '18 at 16:05
  • @kero I've added, as requested. – glvr May 15 '18 at 17:06
  • 1
    Well that's what your error messages are referring to. It's because the $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. – Mat May 16 '18 at 9:16
-3

Works for me

global $wp_query;

if ( function_exists( 'pagination' ) ) {
    pagination( $wp_query->max_num_pages ); 
}else {
    // Previous/next page navigation.
    the_posts_pagination( array(
        'prev_text'          => __( 'Previous' ),
        'next_text'          => __( 'Next' ),
        'screen_reader_text' => __( ' ' )
        //'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page' ) . ' </span>',
    ));
};
  • @RiddleMeThis... thanks for this answer. Appreciated. – glvr May 8 at 6:29
  • This isn't my answer, I just edited it to format it better. bigNY owns this one. – RiddleMeThis May 8 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.