【问题标题】:Next button not showing other posts in Wordpress下一个按钮不显示 Wordpress 中的其他帖子
【发布时间】:2015-07-24 18:34:55
【问题描述】:

在我的 wordpress 中,我有很多帖子,每页显示 5 个帖子。 我的页面底部有下一个和上一个按钮。当我单击一个下一个按钮时,它将转到/page/2/ 链接,但此页面标题显示Page not found。而且它没有在第 2 页显示其他帖子。

我的下一个和上一个代码:

  <div class="prev-next-btn">
             <?php next_posts_link( __( 'next', 'themename' ) ); ?>
            </div>
            <div class="prev-next-btn">
                <?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
            </div>

我的 index.php 代码:

   <div class="center-content">
        <ul>
            <?php query_posts('posts_per_page=5'); ?>
            <?php   if (have_posts()) : while (have_posts()) : the_post(); ?>
          <li>

            <div class="date">In
                <?php
                    $category = get_the_category(); 
                    if(!empty($category))
                        echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';

                    ?> 
                    - <?php the_time('d M, Y') ?>
          </div>
            <!--<div class="auther">by <?php the_author(); ?>  <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
            <div class="title clear-both"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
            <div class="details"><p><?php the_excerpt(); ?></p></div>
            <div class="readmore"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more</a></div>
            <br>
          </li>
            <?php endwhile; ?>

        </ul>
        </div>
          <div class="pagination">
                <?php
                    if(function_exists('wp_pagenavi')) { 
                        wp_pagenavi(); 
                    }
                    else {
                ?>  
            <div class="prev-next-btn">
             <?php next_posts_link( __( 'next', 'themename' ) ); ?>
            </div>
            <div class="prev-next-btn">
                <?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
            </div>
              <?php } ?>
          </div>
      <?php else : ?>
            404 Nothing here. Sorry.
            <?php endif; ?>
      </div>

【问题讨论】:

  • 发布按钮的代码没用,按钮有效。问题可能是您的查询、永久链接结构、SEO 插件冲突...
  • 如果您只向我们提供此信息,则无法回答此问题。我们需要完成上下文,而不仅仅是一小部分
  • 我刚刚添加了我的完整上下文。你能看看
  • 也可能是 .htaccess 问题。
  • .htaccess 中的确切问题是什么。你能告诉我

标签: php wordpress


【解决方案1】:

这里确实有一些问题

  1. 永远不会使用query_posts。它很慢,会重新运行查询,会因分页而无声地中断和失败,更糟糕的是,它会破坏主查询对象。如果你破坏了主要的查询对象,你就破坏了页面功能。所以,请不要使用query_posts。如果它从未存在过,那就做吧

  2. 您已将主查询循环替换为自定义查询,您不能这样做。如果您需要在特定页面上显示不同数量的帖子(不是在页面模板和静态首页上,因为这在那里不起作用),然后使用pre_get_posts。您需要阅读该操作的帮助以及如何使用它

删除这一行

<?php query_posts('posts_per_page=5'); ?>

然后在你的函数文件中添加以下内容

add_action( 'pre_get_posts', function ( $query )
{
    if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'posts_per_page', 5 );
    }
});

【讨论】:

    【解决方案2】:
    <?php
    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    // the query
    $the_query = new WP_Query( 'cat=1&paged=' . $paged ); 
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    <?php
    // the loop
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
    ?>
    <?php the_title(); ?>
    <?php endwhile; ?>
    
    <?php
    
    // next_posts_link() usage with max_num_pages
    next_posts_link( 'Next', $the_query->max_num_pages );
    previous_posts_link( 'Previous' );
    ?>
    
    <?php 
    // clean up after the query and pagination
    wp_reset_postdata(); 
    ?>
    
    <?php endif; ?>
    

    【讨论】:

      【解决方案3】:

      只需添加以下代码:我添加了'paged'变量并设置在next_posts_link()previous_posts_link()..请参见下面的代码:

      <div class="center-content">
              <ul>
                  <?php 
                  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;   
                  query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
                  <?php   if (have_posts()) : while (have_posts()) :the_post(); ?>
                <li>
      
                  <div class="date">In
                      <?php
                          $category = get_the_category(); 
                          if(!empty($category))
                              echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
      
                          ?> 
                          - <?php the_time('d M, Y') ?>
                </div>
                  <!--<div class="auther">by <?php the_author(); ?>  <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
                  <div class="title clear-both"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
                  <div class="details"><p><?php the_excerpt(); ?></p></div>
                  <div class="readmore"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more</a></div>
                  <br>
                </li>
                  <?php endwhile; ?>
      
              </ul>
              </div>
                <div class="pagination">
                      <?php
                      global $wp_query;
                          if(function_exists('wp_pagenavi')) { 
                              wp_pagenavi(); 
                          }
                          else { echo 'test';
                      ?>  
                  <div class="prev-next-btn">
                   <?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
                  </div>
                  <div class="prev-next-btn">
                      <?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
                  </div>
                    <?php } ?>
                </div>
            <?php else : ?>
                  404 Nothing here. Sorry.
                  <?php endif; ?>
            </div>
      

      【讨论】:

      • query_posts 破坏了其他答案中描述的主要查询。
      • 你破坏了主查询,所以使用$wp_query完全没用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      相关资源
      最近更新 更多