0

我遇到的问题是我在自己的页面模板上创建了一个单独的新博客页面,称为 blog.php,我在每个页面中提取了 5 个帖子,第一页效果很好,并链接到它们所附加的单个帖子. 当我尝试将 wp-pagenavi 添加到我的 nav-below 中时,我遇到了问题。发生的事情是我将单击转到下一页并更改 url,但帖子与以前保持不变,当它应该将它们切换到下一组时。我不知道您是否可以在 index.php 之外使用 wp-pagenavi,但如果有人可以让我知道我在这里做错了什么以及为什么我继续收到相同的帖子,这些帖子会很棒并且非常受欢迎。我在 blog.php 上有我的一个博客,这就是我要开始工作的文件。我已经在下面发布了。请让我知道我能做些什么谢谢!

<?php
/**
 * Template Name: Blog Page  
 */
 get_header(); ?>

    <div id="content">
    <?php query_posts("posts_per_page=5"); ?>
    <?php 
    //THE LOOP.
    if( have_posts() ): 
        while( have_posts() ):
        the_post(); ?>

        <article id="post-1" <?php post_class( 'clearfix' ); ?>>
            <h2 class="entry-title"> <a href="<?php the_permalink(); ?>"> 
                <?php the_title(); ?> 
            </a></h2>
            <div class="postmeta"> 
                <span class="author"> Posted by: <?php the_author(); ?> </span> 
                <span class="date"> <?php the_date(); ?> </span> 
                <span class="num-comments"> 
            <?php comments_number('No comments yet', 'One comment', '% comments'); ?></span> 
                <span class="categories"> 
                    <?php the_category(); ?>                
                </span> 
                <span class="tags">
                    <?php the_tags(); ?>
                </span> 
            </div><!-- end postmeta --> 

            <?php if( has_post_thumbnail() ): ?>
            <div class="thumb">
                <?php the_post_thumbnail( 'thumbnail' ); ?>
            </div>
            <?php endif; ?>           

            <div class="entry-content">
                <?php 
            if( is_single()):

                    the_content();

                else:

                    the_excerpt();

                endif;

                ?>
            </div>


        <?php comments_template(); ?>
         </article><!-- end post -->
      <?php 
      endwhile;
      else: ?>
      <h2>Sorry, no posts found</h2>
      <?php endif; //END OF LOOP. ?>


        <div id="nav-below" class="pagination"> 
<?php if(function_exists('wp_pagenavi')) // if PageNavi is activated ?>

<?php wp_pagenavi(); // Use PageNavi ?>

        </div><!-- end #nav-below --> 
    </div><!-- end content -->

<?php get_footer(); ?>  
4

2 回答 2

0

尝试这个 :

<?php $paged = 1;
 if ( get_query_var('paged') ) $paged = get_query_var('paged');
 if ( get_query_var('page') ) $paged = get_query_var('page');
 $temp = $wp_query; 
 $wp_query = null; 
 $wp_query = new WP_Query(); 
 $wp_query->query('posts_per_page=5'.'&paged='.$paged);?>
 <?php if ( $wp_query->have_posts() ) : ?>
     //your loop code goes here
   <?php wp_pagenavi(); ?>
  <?php endif; // end have_posts() check ?>
 <?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
 ?>
于 2013-05-02T19:40:16.493 回答
0

这是我认为你应该做的:

第 1 步:创建一个名为“Blog”的页面(假设您已经这样做了)

第 2 步:丢弃blog.php您创建的模板

第 3 步:进入管理 -> 设置 -> 阅读,为“首页显示”选择“静态页面”,然后从“帖子页面”下拉列表中选择“博客”页面。

第 4 步:复制您的index.php文件,并将其重命名为home.php. 这将是应用于您的新“主页”页面的模板。home.php然后用wp-pagenavi 代码替换默认分页。

第 5 步:修改您全新博客页面上的查询,将其放入您的functions.php文件中:

add_action( 'pre_get_posts','so16345510_pre_get_posts' );
function so16345510_pre_get_posts( $query )
{
    if( is_home() && $query->is_main_query() ){
        $query->set( 'posts_per_page', 5 );
    }
    return $query;
}
于 2013-05-02T19:30:51.760 回答