Show pagination in WP_Query

时间:2018-03-14 作者:Ale

我正在编写一个foodblog,并试图在特定页面中显示收藏夹帖子列表,但我对分页有一些问题。我在前面的问题中尝试了不同的解决方案,但都没有解决问题。非常感谢

 <?php $my_favs = get_user_meta(get_current_user_id(), \'user_favs\', true);

         $args = array(
                \'post_type\' => array(\'recipe\', \'post\'),
                \'post__in\'  => $my_favs,
                \'post_author\' => get_current_user_id(),
                \'pagination\'             => true,
                \'posts_per_page\'         => -1
            );

            $recipes = new WP_Query( $args );
 while ( $recipes->have_posts() ) : $recipes->the_post(); ?>

                        <a href="<?php the_permalink();?>">
                            <h2>
                              <?php $title = get_the_title();
                                echo wp_trim_words($title, 4);?> 
                            </h2>
                        </a>
                        <p>
                            <?php $content = get_the_content();
                             echo wp_trim_words($content, 9, "...");?> 
                        </p>

          <?php endwhile; // End of the loop.?>
            <div class="pagination">
                <?php 
                    echo paginate_links();
                ?>
            </div>


            <?php wp_reset_postdata(); ?>   

1 个回复
SO网友:mattbru

这是我使用的分页函数。我不确定github链接是否是原始信用,但我找到了它。将以下内容添加到函数中。php文件:

/**
 * Creates Custom Pagination
 * @link https://gist.github.com/rgfx/755cfb71fd1732e4e7b7bdcbd4b6e4e3
 * @param  string $numpages  [description]
 * @param  string $pagerange [description]
 * @param  string $paged     [description]
 * @return html
 */
function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {


if (empty($pagerange)) {
    $pagerange = 2;
}

/**
 * This first part of our function is a fallback
 * for custom pagination inside a regular loop that
 * uses the global $paged and global $wp_query variables.
 * 
 * It\'s good because we can now override default pagination
 * in our theme, and use this function in default quries
 * and custom queries.
 */
global $paged;
if (empty($paged)) {
    $paged = 1;
}
if ($numpages == \'\') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
            $numpages = 1;
    }
}

/** 
 * We construct the pagination arguments to enter into our paginate_links
 * function. 
 */
$pagination_args = array(
    \'base\'            => get_pagenum_link(1) . \'%_%\',
    \'format\'          => \'page/%#%\',
    \'total\'           => $numpages,
    \'current\'         => $paged,
    \'show_all\'        => False,
    \'end_size\'        => 1,
    \'mid_size\'        => $pagerange,
    \'prev_next\'       => True,
    \'prev_text\'       => __(\'\'),
    \'next_text\'       => __(\'\'),
    \'type\'            => \'plain\',
    \'add_args\'        => false,
    \'add_fragment\'    => \'\'
);

$paginate_links = paginate_links($pagination_args);

if ($paginate_links) {
    echo "<nav class=\'custom-pagination\'>";
        echo "<span class=\'page-numbers page-num\'>Page " . $paged . " of " . $numpages . "</span> ";
        echo $paginate_links;
    echo "</nav>";
}
}\'

然后,在存档模板中,在wp_reset_postdata(); :

<!-- pagination here -->
<?php if (function_exists(custom_pagination)) {
    custom_pagination($post_query->max_num_pages,"",$paged);
} ?>
此外,这可能取决于您所使用的页面模板。Im采用自定义模板或归档页面模板。

接下来,如果您仍然有问题,那么我建议检查$paged变量,如下所示:

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$suri = $_SERVER[\'REQUEST_URI\'];
    // check $paged variable
    echo \'<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">\';
    print_r($paged);
    echo \'</pre>\';
// grab the last number from the url to use as the $paged variable
$exp_suri = explode(\'/\',$suri);
$page_query = $exp_suri[2];
$page_num = substr($page_query, -1);
$paged = is_numeric($page_num) ? $page_num : $paged;
    // Now, check $paged variable again
    echo \'<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">\';
    print_r($paged);
    echo \'</pre>\';
您可能可以删除或注释掉第二部分(结尾是exp\\u suri)。这里的前置标签是display:none, 因此,要么将其从内联样式中去掉,要么使用开发工具显示它。

最后,如果你还卡住了,我会读WordPress pagination trouble shooting, here.

结束

相关推荐

Custom posts - tag pagination

我使用以下代码输出属于tag:add_filter(\'pre_get_posts\', \'query_post_type\'); function query_post_type($query) { if(is_category() || is_tag() && empty( $query->query_vars[\'suppress_filters\'] ) ) { $post_type = get_query_var(\'pos