【问题标题】:WordPress Pagination Won't Switch PagesWordPress分页不会切换页面
【发布时间】:2017-12-12 20:11:04
【问题描述】:

一个多星期以来,我一直在寻找这个问题的解决方案,但我找不到其他有同样问题的人。

我正在开发其他人构建的自定义 WP 主题。有一个单页模板,我需要为其中一个辅助循环实现分页。我一直在尝试使用内置的paginate_links() 函数以及其他方法。显示分页链接,但是当我单击分页链接时,它不会转到分页中的该页面。而是重新加载原始页面(即,它不是转到 thewebsite.com/my-page/page/2/,而是重新加载 thewebsite.com/my-page/)。

之前的开发者在functions.php中使用了这个过滤器来加载正确的模板:

add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $t;' ));

这是我的模板文件:

<?php
/**
* Template Name: Project Template
*/
get_header('news'); ?>

<article role="main" class="projectpage">
<div class="container">
  <section class="pagecontent">


      <?php if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>
      <section class="overview">
          <h1><?php the_title(); ?></h1>
          <div>
              <?php the_content(); ?>
          </div>
          <div>
              <?php if(get_post_meta($post->ID, 'pagelink', true)): ?>
                  <a href ="<?php echo get_post_meta($post->ID, 'pagelink', true); ?>" class="ctabutton2">  Read the Overview </a>
              <?php endif; ?>
          </div>

</div><!--end row-->
   </section><!--end overview-->
   <?php endwhile ?>
   <?php wp_reset_postdata() ?>
   <? endif ?>


   <section class="related">
       <div>
           <h1> Related Resources </h1>
           <h2> Explore our library of articles and resources </h2>
       </div>
       <div class="row">
           <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relatedlinks">
               <section class="projectcategories">
                   <h3> Categories </h3>
                   <ul>
                       <?php wp_list_categories( array(
                           'orderby'            => 'id',
                           'show_count'         => true,
                           'use_desc_for_title' => false,
                           'child_of'           => 93,
                           'title_li' => ' '
                       ) ); ?>
                   </ul>
               </section>

               <section class="project-search" role="search">

                   <form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                       <input type="hidden" name="cat" id="cat" value="93" />
                       <input type="text" size="16" name="s" placeholder="search keywords" class="search-box" />
                       <input type="submit" value="Go" class="go"/>
                   </form>

               </section>

               <section class="otherprojects">
                   <h3> Other Projects </h3>

                   <?php
                   $args = array(
                       'category__in' => 91,
                       'post__not_in' => array( $post->ID )
                   );
                   // the query
                   $query = new WP_Query( $args );
                   $temp_query = $wp_query;
                   $wp_query = NULL;
                   $wp_query = $query;

                   // The Loop
                   if ( $query->have_posts() ) :  while ( $query->have_posts() ) : $query->the_post(); ?>
                       <a href="<?php the_permalink();?>"><?php the_title(); ?></a>

                   <? endwhile ;
                       /* Restore original Post Data */
                       wp_reset_postdata();
                   endif;


                   $wp_query = NULL;
                   $wp_query = $temp_query;
                   ?>
               </section>
           </div><!--end col 1-->

          <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
            <section class="articles">

              <?php
//  THIS IS THE SECTION WHERE I NEED THE PAGINATION
              $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

              $args = [
                'posts_per_page' => 3,
                'paged' => $paged,
                'post_type' => 'post',
                'order' => 'DESC',
                'post__not_in' => array( $post->ID ),
                'tax_query' => [
                [
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => '93',
                ],
              ],
            ];

            $custom_query = new WP_Query( $args );
              $temp_query = $wp_query;
              $wp_query = NULL;
              $wp_query = $custom_query;
                                if ( $custom_query->have_posts() ) {
                                    while ( $custom_query->have_posts() ) {
                                        $custom_query->the_post(); ?>
                                    <div class="row">
                                         <div class="col-md-2 col-sm-2 col-xs-2 divider">
                                                <p class="date"><?php the_time('M j') ?></p>
                                            </div><!--end col-->
                                            <div class="col-md-4 col-sm-4 col-xs-4">
                                                <div class="articleimg">
                                                <?php if ( has_post_thumbnail()) {?>
                                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a>
                                                <?php } ?>
                                                </div><!--end blogimg-->
                                            </div><!--end col-->
                                            <div class="col-md-6 col-sm-6 col-xs-6">
                                                <div class="blogcontent">
                                                    <h3><?php the_title();?></h3>
                                                    <p><?php the_excerpt(); ?></p>
                                                    <a href="<?php the_permalink(); ?>" class="readmore"> // read more </a>
                                                </div><!--end blogcontent-->
                                            </div><!--end col-->
                                    </div><!--end row-->
            <?php }
                }

          echo paginate_links(array(
              'total' => $wp_query->max_num_pages
          ));


              $wp_query = NULL;
              $wp_query = $temp_query;
              wp_reset_postdata(); ?>
            </section><!--end articles-->
          </div><!--end col 2-->
        </div> <!--end row-->
      </section><!--end related-->


<!--   ANNOUNCEMENTS    -->
   <!--ANNOUNCEMENT SECTION -->
   <!-- dynamic content --filters posts by category and only shows 'member' posts with a limit of six posts being
   displayed-->
   <section id="announcement-front" class="clearfix">
       <div class="container">
           <div>
               <?php $query = new WP_Query('posts_per_page=1&category_name=advertisement');
               if ($query->have_posts()) :
                   while ($query->have_posts()) : $query->the_post(); ?>
                       <a href="<?php the_permalink()?>" <?php the_content();?> </a>
                   <?php endwhile ?>
               <? endif ?>
               <?php wp_reset_postdata() ?>
           </div><!--end row-->
       </div><!--container-->
   </section><!--end announcement-->


  </section> <!--end page content -->
</div><!--end container-->
        </article>
<?php get_footer(); ?>

我意识到那里有大量的 WordPress 分页教程和线程,但我还没有找到一个可以解决这个特殊问题的。

【问题讨论】:

    标签: php wordpress templates pagination


    【解决方案1】:

    我认为您的代码应该可以正常工作。你不必担心$wp_query 的所有保存和切换——你试过这个更简单的sn-p 吗?它在我的环境中运行良好(尽管我必须稍微修改查询参数以使其返回任何结果)。

    如果这不起作用,您可以发布它生成的 HTML 吗?

            <section class="articles">
              <?php
    //  THIS IS THE SECTION WHERE I NEED THE PAGINATION
              $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
              $args = [
                'posts_per_page' => 3,
                'paged' => $paged,
                'post_type' => 'post',
                'order' => 'DESC',
                'post__not_in' => array( $post->ID ),
                'tax_query' => [
                [
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => '93',
                ],
              ],
            ];
    
            $custom_query = new WP_Query( $args );
                                if ( $custom_query->have_posts() ) {
                                    while ( $custom_query->have_posts() ) {
                                        $custom_query->the_post(); ?>
                                    <div class="row">
                                         <div class="col-md-2 col-sm-2 col-xs-2 divider">
                                                <p class="date"><?php the_time('M j') ?></p>
                                            </div><!--end col-->
                                            <div class="col-md-4 col-sm-4 col-xs-4">
                                                <div class="articleimg">
                                                <?php if ( has_post_thumbnail()) {?>
                                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a>
                                                <?php } ?>
                                                </div><!--end blogimg-->
                                            </div><!--end col-->
                                            <div class="col-md-6 col-sm-6 col-xs-6">
                                                <div class="blogcontent">
                                                    <h3><?php the_title();?></h3>
                                                    <p><?php the_excerpt(); ?></p>
                                                    <a href="<?php the_permalink(); ?>" class="readmore"> // read more </a>
                                                </div><!--end blogcontent-->
                                            </div><!--end col-->
                                    </div><!--end row-->
            <?php }
                }
    
          echo paginate_links(array(
              'total' => $custom_query->max_num_pages
          ));
    
              wp_reset_postdata(); ?>
            </section><!--end articles-->
          </div><!--end col 2-->
        </div> <!--end row-->
      </section><!--end related-->
    

    【讨论】:

    • 谢谢你的建议,乔。这实际上是我最初使用的代码,然后阅读了有关保存和切换$wp_query 的教程并尝试了一下。生成的分页链接似乎是正确的:&lt;span class="page-numbers current"&gt;1&lt;/span&gt; &lt;a class="page-numbers" href="http://thewebsite.com/my-page/page/2/"&gt;2&lt;/a&gt; &lt;a class="page-numbers" href="http://thewebsite.com/my-page/page/3/"&gt;3&lt;/a&gt; &lt;a class="next page-numbers" href="http://thewebsite.com/my-page/page/2/"&gt;Next »&lt;/a&gt; 但是当我单击它们时,它只会加载原始页面。
    • 啊,我明白了。问题不是分页链接,而是当您单击它们时页面被重定向的方式。首先尝试刷新您的永久链接(转到设置 -> 永久链接并单击保存),如果这不起作用,请尝试此链接以获取想法? wordpress.org/support/topic/…
    【解决方案2】:

    谢谢,乔!那篇文章为我指明了正确的方向。我检查了开发工具,确实得到了 301。我尝试了您指出的文章中的代码 sn-p,但效果不佳,因此我在 Google 上搜索了“使用 redirect_canonical 修复分页”和this was the first article that popped up。我从那里获取函数,将其放入functions.php,etvoilà!这是相同的方法,但是,我认为,没有将自定义帖子类型传递给条件。我希望这可以帮助将来的人。这是一个真正的痛苦。这也提醒了我对 WP 的了解是多么的少,而我有多么想了解它!再次感谢。

    代码如下:

    add_filter('redirect_canonical','custom_disable_redirect_canonical');
    function custom_disable_redirect_canonical($redirect_url) {
        if (is_paged() && is_singular()) $redirect_url = false;
        return $redirect_url;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      相关资源
      最近更新 更多