分页显示相同的帖子,尽管页面正在更改

时间:2014-09-15 作者:Adrian

我从“高级”Wordpress主题中获取了以下分页代码,试图将其集成到自定义主题中,但似乎无法使其正常工作:

在博客中。php页面模板,我有循环:

                $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
                $args=array(
                        \'paged\' => $paged,
                        \'post_type\' => \'post\',
                        \'posts_per_page\' => \'1\',
                        \'order\' => \'DESC\',
                        \'orderby\' => \'date\',
                );
                 $wp_query = new WP_query($args);
                 if ( $wp_query -> have_posts() ) :
                 $j = 1;
                 while ($wp_query -> have_posts()): $wp_query -> the_post();?>
                 CONTENT HERE
                 <?php endwhile;prestigo_SPaginate();endif;?>
其功能是:

if(!function_exists(\'prestigo_SPaginate\')){
        function prestigo_SPaginate()
        {
            $currentPage = null;
            $totalPage = null;
            global $wp_query;           
                $currentPage = intval(get_query_var(\'paged\'));
            if(empty($currentPage))
            {
                $currentPage = 1;
            }        
            $totalPage = intval(ceil($wp_query->found_posts / intval(get_query_var(\'posts_per_page\'))));
            if($totalPage <= 1)
            {
                return \'\';
            }
            $paginateResult = \'<!-- PAGINATION -->
                            <ul class="pagination clearfix">\';

            if ($currentPage > 1)
            {
                $paginateResult .= \'<li class="pull-left"><a href="\'.get_pagenum_link($currentPage - 1).\'"><i class="fe arrow_carrot-left"></i></a></li>\';
            }elseif ($currentPage = 1)
            {
                $paginateResult .= \'<li class="pull-left"><a href="javascript:void(0);"><i class="fe arrow_carrot-left"></i></a></li>\';
            }
            $paginateResult .= prestigo_ListLink(1, $totalPage, $currentPage);
            if ($currentPage < $totalPage)
            {
                $paginateResult .= "<li class=\'pull-right\'><a href=\'" . get_pagenum_link($currentPage + 1) . "\' class=\'spaginate-next\'><i class=\'fe arrow_carrot-right\'></i></a></li>";
            }elseif($currentPage = $totalPage){
                $paginateResult .= "<li class=\'pull-right\'><span class=\'spaginate-next\'><i class=\'fe arrow_carrot-right\'></i></span></li>";
            }
            $paginateResult .= "</ul><!-- //PAGINATION -->";
            echo $paginateResult;
            return $paginateResult;
        }
    }

    if(!function_exists(\'prestigo_ListLink\')){
        function prestigo_ListLink($intStart, $totalPage, $currentPage)
        {
            $pageHidden = \'<span class="spaginate-hidden">... </span>\';
            $linkResult = "";
            $hiddenBefore = false;
            $hiddenAfter = false;
            for ($i = $intStart; $i <= $totalPage; $i++)
            {
                if($currentPage === intval($i))
                {
                    $linkResult .= \'<li class="active"><a href="\'.get_pagenum_link($i).\'">\'.$i.\'</a></li>\';
                }
                else if(($i <= 6 && $currentPage < 10) || $i == $totalPage || $i == 1 || $i < 4 || ($i <= 6 && $totalPage <= 6) || ($i > $currentPage && ($i <= ($currentPage + 2))) || ($i < $currentPage && ($i >= ($currentPage - 2))) || ($i >= ($totalPage - 2) && $i < $totalPage))
                {
                    $linkResult .= \'<li><a href="\'.get_pagenum_link($i).\'">\'.$i.\'</a></li>\';
                    if($i <= 6 && $currentPage < 10)
                    {
                        $hiddenBefore = true;
                    }
                }
                else
                {
                    if(!$hiddenBefore)
                    {
                        $linkResult .= $pageHidden;
                        $hiddenBefore = true;
                    }
                    else if(!$hiddenAfter && $i > $currentPage)
                    {
                        $linkResult .= $pageHidden;
                        $hiddenAfter = true;
                    }
                }
            }
            return $linkResult;
        }
    }
不幸的是,分页显示了出来,但单击第2页显示的帖子与第1页显示的帖子相同。

任何建议都很好,谢谢。

3 个回复
最合适的回答,由SO网友:Adrian 整理而成

对于这些天的分页,我使用以下内容:

function wpbeginner_numeric_posts_nav() {

if( is_singular() )
    return;

global $wp_query;

/** Stop execution if there\'s only 1 page */
if( $wp_query->max_num_pages <= 1 )
    return;

$paged = get_query_var( \'paged\' ) ? absint( get_query_var( \'paged\' ) ) : 1;
$max   = intval( $wp_query->max_num_pages );

/** Add current page to the array */
if ( $paged >= 1 )
    $links[] = $paged;

/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
}

echo \'<div class="navigation"><ul>\' . "\\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
    printf( \'<li>%s</li>\' . "\\n", get_previous_posts_link() );

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? \' class="active"\' : \'\';

    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( 1 ) ), \'1\' );

    if ( ! in_array( 2, $links ) )
        echo \'<li>…</li>\';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
    $class = $paged == $link ? \' class="active"\' : \'\';
    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
    if ( ! in_array( $max - 1, $links ) )
        echo \'<li>…</li>\' . "\\n";

    $class = $paged == $max ? \' class="active"\' : \'\';
    printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}

/** Next Post Link */
if ( get_next_posts_link() )
    printf( \'<li>%s</li>\' . "\\n", get_next_posts_link() );

echo \'</ul></div>\' . "\\n";

}

SO网友:johangiraud

在为此苦苦挣扎了几个小时后(以及问题提出后的6年),对我来说,解决方案就在$paged参数范围内:

我改变了

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;

if ( get_query_var(\'paged\') ) {
      $paged = get_query_var(\'paged\');
    } else if ( get_query_var(\'page\') ) {
      $paged = get_query_var(\'page\');
    } else {
      $paged = 1;
    }
然后它成功了!

SO网友:Caleb

尝试更改paged 参数和查询变量;我在过去经历过变量与法典所说的不同。尝试切换paged 具有pageget_query_var 以及WP_Query args。

结束

相关推荐

foreach pagination

我正在寻求有关如何对foreach输出分页的帮助。我查看了其他问题和答案,但找不到适合我的解决方案,也找不到可以自己解决的解决方案。现在,下面的代码将所有内容输出到表行中。当然,我的问题是,它将所有数据转储到一个页面上——因此我需要分页。我想为页面上的每11项分页。这一页是一份杂志档案,每年有11期——因此每一页相当于我们杂志的1年。第一页应该是第1-11期,第二页应该是第12-22期,等等。我们有10年的杂志期。任何帮助都将不胜感激。非常感谢。<table> <tr>&#