在页面模板上具有工作分页的自定义wp_query [英] Custom wp_query with working pagination on a page template

查看:60
本文介绍了在页面模板上具有工作分页的自定义wp_query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向WordPress模板添加自定义查询并包含分页,但是我的分页没有出现,例如,正试图将其添加到page.php.

I'm trying to add a custom query to a WordPress template and include pagination but my pagination isn't appearing, for example's sake I'm trying to add this to page.php.

我有以下标记,当放置在类别模板(如category.php)中时,分页显示出来并且功能正常.问题是,当在page.php或任何自定义页面模板中放置相同的代码时,分页不会出现.

I have the following markup which works perfectly when place inside a category template like category.php, the pagination shows up and functions just fine. The issue is that the pagination doesn't appear when the same code is place in page.php or any custom page template.

查询:

              <?php
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $loop = new WP_Query(
                    array(
                        'post_type' => 'post',
                        'posts_per_page' => 10,     
                        'paged' => $paged,
                        'orderby' => 'desc',
                    )
                );
              ?>

              <?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>

                // Loop Markup goes here.

              <?php endwhile; ?> 
              <?php endif; ?>

              <?php wp_reset_query(); ?>

              <?php pagination(); ?>

在functions.php中定义的Pagination():

              function pagination() {
                  global $wp_query;
                  $big = 999999999;
                  echo paginate_links(array(
                      'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                      'format' => '?paged=%#%',
                      'current' => max(1, get_query_var('paged')),
                      'total' => $wp_query->max_num_pages
                  ));
              }
              add_action('init', 'pagination');

我看到一些帖子要求有关同一主题的帮助,但是我没有遇到一个优雅的解决方案.

I've seen a few posts requesting help on the same subject, but I didn't come across an elegant solution.

任何建议将不胜感激!我对PHP的了解非常有限,我采用了Todd Motto的HTML5 Blank主题中的pagination()函数,所以我不是100%理解该函数的定义.

Any advice would be much appreciated! My knowledge of PHP is pretty limited, I took the pagination() function from the HTML5 Blank theme by Todd Motto so I don't 100% understand what that function defines.

推荐答案

通过将原始帖子中的代码与

Managed to find a solution by merging my code from the original post with the following code from this tutorial:

我正在为一个丢失任何人并且需要更多上下文来实现此目的的人张贴一个简单page.php的完整示例,这对我来说非常有效,没有像每个页码返回相同帖子或其他内容这样的残缺方面.

I'm posting a complete example of a simple page.php for anyone lost and needing more context to implement this, this works perfectly for me with no broken aspects like each page number returning the same posts or anything.

<?php get_header(); ?>

    <?php if (have_posts()): while (have_posts()) : the_post(); ?>

            <div class="the_loop">

              <?php        
                if ( get_query_var('paged') ) {
                    $paged = get_query_var('paged');
                } elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
                    $paged = get_query_var('page');
                } else {
                    $paged = 1;
                }
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $loop = new WP_Query(
                    array(
                        'post_type' => 'post',
                        'posts_per_page' => get_option('posts_per_page'),     
                        'paged' => $paged,
                        'post_status' => 'publish',
                        'orderby' => 'desc',
                        'orderby' => 'date' // modified | title | name | ID | rand
                    )
                );
              ?>

              <?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>

                // Loop code goes here.

              <?php endwhile; ?>

              <?php if ($loop->max_num_pages > 1) : // custom pagination  ?>
                <?php
                  $orig_query = $wp_query; // fix for pagination to work
                  $wp_query = $loop;
                  $big = 999999999;
                  echo paginate_links(array(
                      'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                      'format' => '?paged=%#%',
                      'current' => max(1, get_query_var('paged')),
                      'total' => $wp_query->max_num_pages
                  ));                  
                  $wp_query = $orig_query; // fix for pagination to work
                ?>
              <?php endif; ?>

              <?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>  

            </div>

    <?php endwhile; ?>
    <?php endif; ?>    

<?php get_template_part('footer'); ?>

这篇关于在页面模板上具有工作分页的自定义wp_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆