wp-pagenavi not working… It changes the actual url

2019-09-18 11:05发布

问题:

The issue I am having is I have created a separate new blog page on its own page template being called blog.php, I have pulled 5 posts into each page and the first page work great and link to the single posts that they are attached too. When I try to add wp-pagenavi into my nav-below I run into issues. What is happening is I will click to go the next page and it changes the url, but the posts remain the same as before, when it should be switching them to the next set. I don't know if you can use wp-pagenavi outside of index.php but if anyone can let me know what I am doing wrong here and why I continue to get the same posts that would be awesome and greatly appeciated. I have one of my blogs on blog.php and that is the file I am trying to get to work. I have posted that below. Please let me know what I can do thank you!

<?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(); ?>  

回答1:

Here's what I think you should do:

step 1: create a page called 'Blog' (presumably, you did this already)

step 2: throw away the blog.php template you created

step 3: Go to admin -> settings -> reading, select "a static page" for "front page displays", and select the page "Blog" from the "Posts page" dropdown.

step 4: make a copy of your index.php file, and rename it to home.php. This will be the template applied to your new "Home" page. Then replace the default pagination in home.php with the wp-pagenavi code.

step 5: modify the query on your brand new blog page by putting this in your functions.php file:

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;
}


回答2:

Try this :

<?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
 ?>