我如何修复这个“未定义的变量:”调试消息?

时间:2018-05-15 作者:glvr

下面的代码(我在函数中使用)来自https://stackoverflow.com/questions/35263445/numeric-pagination-on-my-custom-category-page, 并显示“Undefined variable:additional\\u loop”和“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";
} }
还有。。。

if (function_exists( \'paginate\' ) ) {
paginate( $additional_loop->max_num_pages );
}
我需要更改什么来修复它?

@kero在评论中要求包含更多我正在使用的代码,下面是查询(位于页面模板中(以及“if function\\u exists”内容)。

$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,
) );

1 个回复
SO网友:bigNY

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>\',
    ));
};
结束

相关推荐

Pagination in Archives

你好,我是wordpress的新手,我很烂。我试图在wordpress中为我的博客页面添加编号分页。我下载了插件“page navi”,进入编辑器并更改了索引中的一个文件。php收件人: <?php if ( $wp_query->max_num_pages > 1 ) : ?> <div class=\"post-nav archive-nav\"> <?php wp_pagenavi();