Wordpress分页列表显示错误的页数?

时间:2013-12-17 12:16:15

标签: wordpress pagination custom-taxonomy

我已经为自定义分类创建了一个分页的术语列表,但分页没有正确显示。无论我设置的每页有多少帖子,都只输出两页。

因此,对于6个帖子并设置为每页6个,我看到两个页面,第二个是空白。有6个帖子,每页2个,我看到两个页面,没有第三页(我尝试在网址栏中输入/ page / 3 /,找不到页面,所以这不仅仅是分页按钮的问题但似乎是Wordpress输出的页数有问题。

有人能看出为什么会这样吗?如何让分页正常工作?

如果有人能提出让这种分页正常工作的方法,我已经为这个问题添加了赏金。


更新19/12:

我是部分解决这个问题的方法。我发现部分问题似乎部分与WP中的阅读设置有关。我的阅读设置设置为每页6,这就是为什么无论我在模板文件中$posts_per_page设置什么,我只得到2页的输出。

现在我可以让Wordpress输出所有页面和分页链接,如果我确保读取设置与模板文件中设置的数字匹配,但我总是从for循环获得额外的输出,导致空div 。这会在某些情况下导致额外的页面,具体取决于最后一页上的帖子数量。我在最后一页上也没有分页链接。


archive-prints.php的代码摘录:

$posts_per_page = 6;

$page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $page - 1 );

$categories = get_terms('prints_cat');

for( $i = $offset * $posts_per_page; $i < ( $offset + 1 ) * $posts_per_page; $i++ ) {
    $category = $categories[$i];
    echo '<div class="cat-preview"><a href="';
    echo get_term_link($category->slug, 'prints_cat');
    echo '"><h2>';
    echo $category->name;
    echo '</h2></a></div>';
}
unset( $category );

custom_page_navi();

我的functions.php文件中的custom_page_navi()函数的代码:

function custom_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;

echo '<nav class="pagination">';

    echo paginate_links( array(
        'base'          => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
        'format'        => '',
        'current'       => max( 1, get_query_var('paged') ),
        'total'         => $wp_query->max_num_pages,
        'prev_text'     => 'Prev',
        'next_text'     => 'Next',
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => 2,
        'mid_size'      => 0
    ) );

echo '</nav>';

}

2 个答案:

答案 0 :(得分:3)

您可以使用以下代码创建自定义页面模板文件tpl_list.php

<?php
/**
 * Template Name: Paginated list of terms for a custom taxonomy
 *
 */

// Edit:
$taxonomy = 'prints_cat';
$number   = 3; // number of terms to display per page

// Setup:
$page         = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset       = ( $page > 0 ) ?  $number * ( $page - 1 ) : 1;
$totalterms   = wp_count_terms( $taxonomy, array( 'hide_empty' => TRUE ) ); 
$totalpages   = ceil( $totalterms / $number );

// Debug:
// printf( 'taxonomy: %s - number: %s - page: %s - offset: %s - totalterms %s - totalpages: %s' , $taxonomy, $number, $page, $offset, $totalterms, $totalpages );

// Here I list all the available paramters to get_terms():
$args = array(
    'orderby'       => 'name', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
    'exclude'       => array(), 
    'exclude_tree'  => array(), 
    'include'       => array(),
    'number'        => $number, 
    'fields'        => 'all', 
    'slug'          => '', 
    'parent'         => '',
    'hierarchical'  => true, 
    'child_of'      => 0, 
    'get'           => '', 
    'name__like'    => '',
    'pad_counts'    => false, 
    'offset'        => $offset, 
    'search'        => '', 
    'cache_domain'  => 'core'
); 

$terms = get_terms( $taxonomy, $args );

foreach ( $terms as $term )
{
    printf( '<div class="cat-preview"><h2><a href="%s">%s</a></h2></div>',
            get_term_link($term->slug, 'country'),
            $term->name,
            $term->name 
        );
}

// Show custom page navigation
printf( '<nav class="pagination">%s</nav>', 
            custom_page_navi( $totalpages, $page, 3, 0 ) 
          );

,其中

function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

    return paginate_links( array(
        'base'          => str_replace( $bignum, '%#%', esc_url( get_pagenum_link( $bignum ) ) ),
        'format'        => '',
        'current'       => max( 1, $page ),
        'total'         => $totalpages,
        'prev_text'     => 'Prev',
        'next_text'     => 'Next',
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );
}

创建一个页面(例如名为prints)并选择上面的页面模板。

然后你可以访问:

   example.com/prints/
   example.com/prints/page/2/
   example.com/prints/page/3/

如果您取消注释调试行,您将获得例如:

taxonomy: prints_cat - 
number: 3 - 
page: 2 - 
offset: 3 - 
totalterms 6 - 
totalpages: 2

答案 1 :(得分:0)

的价值

'end_size' => 2, 'mid_size' => 0

与codex中提到的默认值不同,您可以尝试将它们更改为

'end_size' => 1, 'mid_size' => 2

相关问题