最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pagination - How do I fix this 'Undefined variable:' debug message?

programmeradmin0浏览0评论

The code below (which I use in functions) is from , 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,
) );

The code below (which I use in functions) is from https://stackoverflow/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,
) );
Share Improve this question edited May 15, 2018 at 17:05 glvr asked May 15, 2018 at 14:40 glvrglvr 8813 gold badges11 silver badges28 bronze badges 8
  • What is $additional_loop? If your code is the same from the OP, then it should probably be $custom_query – kero Commented May 15, 2018 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 Commented May 15, 2018 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 Commented May 15, 2018 at 16:05
  • @kero I've added, as requested. – glvr Commented May 15, 2018 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 Commented May 16, 2018 at 9:16
 |  Show 3 more comments

1 Answer 1

Reset to default -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>',
    ));
};
发布评论

评论列表(0)

  1. 暂无评论