页面中的帖子分页不起作用wordpress

时间:2015-12-11 08:36:45

标签: php wordpress pagination

<?php paginate_posts(); ?>

上述代码的分页在分类法类别页面中有效。 但是当我在页面Cannot modify header information - headers already sent by中调用此函数时,它无效。但无法理解是什么问题?

1 个答案:

答案 0 :(得分:0)

为了让我的分页代码在页面内部工作,我将代码放在我要显示分页的页面内,然后我用我的参数添加了新查询,现在它可以工作了。我的最终代码如下:

<?php
    global $wp_query, $wp_rewrite;
    $wp_query = new WP_Query($args);
    $wp_query->query('post_type=product&paged='.get_query_var('paged').'&odrerby=ID&posts_per_page=15&order=DESC');
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $current = str_pad($current, 2, '0', STR_PAD_LEFT);
    $total = str_pad($wp_query->max_num_pages, 2, '0', STR_PAD_LEFT);
    $pagination_args = array(
        'base' => @add_query_arg('page', '%#%'),
        'format' => '',
        'total' => $wp_query->max_num_pages,
        'current' => $current,
        'show_all' => false,
        'end_size' => 0,
        'mid_size' => 0,
        'next_text' => _("&raquo;"),
        'prev_text' => _("&laquo;"),
    );
    if ($wp_rewrite->using_permalinks())
        $pagination_args['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');

    if (!empty($wp_query->query_vars['s']))
        $pagination_args['add_args'] = array('s' => get_query_var('s'));
    $prev = get_previous_posts_link('');
    $next = get_next_posts_link('');
    $links = "<span class='pagexofy'><span class='prev page-numbers'>$prev</span> <span>PAGE $current/$total</span> <span class='next page-numbers'>$next</span></span>";
    echo $links;
?>