• Fact: My blog home page has a custom query.

    Fact: I installed pagenavi plugin to add pagination to my blog home page, but when I tried to click on the pagenavi controls to get to the second page of the blog, I get the index.php returned with a “Page not found – No content is appearing for this page” message, even though there are more blogs to be displayed.

    Fact: I know this has been discussed a lot.. and I’ve read and tried many suggestions.. just no joy for me.. so I am hoping someone can help me.

    Here is the code that gets my blog posts for my Posts Page (this code is on home.php, because front-page.php is used for my static Front Page).

    		<?php
    			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    			$myquery = new WP_Query(
    				array(
    					'post_type' => 'post',
    					'post_status' => 'publish',
    					'order'=> 'DESC', 
    					'orderby' => 'post_date', 
    					'category_name' => 'Recipes',
    					'posts_per_page' => 1,
    					'paged' => $paged,
    					// add any other parameters to your wp_query array
    				)   
    			);  
    			?>
    
    		<?php
    		if ($myquery->have_posts()) :  while ($myquery->have_posts()) : $myquery->the_post();
    		?>
    
    		<!-- Start your post -->
    
    				<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 home-blog-list" style="float: left; display:block">				
    					<center><div class="img-responsive box-shadow shadow-effect" style=""><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
    					<h3 class="entry-title script" style="text-align: center;"><a class="entry-title-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><center>
    				</div>
    
    		<!-- End of your post -->
    
    		<?php endwhile; ?>
    		<?php wp_pagenavi( array( 'query' => $myquery ) ); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
    		<?php wp_reset_query(); ?>
    		<?php else : ?>
    		<p>No posts found</p>
    		<?php endif; ?>	
    

    This all works fine as far as displaying the posts. This version of code I am using has some code that was suggested to help the issue (of ‘page not found’ with pagenavi pagination), but I still have the problem. (Also note, I am developing the site, and only have 3 blogs created so far, so in order to test pagination, I set my code to just display one post per page, so I can get 3 pages to test.)

    My settings/readings is set to- “Blog pages show at most: 9”. (I heard that could be a problem also. (And yes, I have saved the page to reset the permalinks).

    I have also tried adding this code to the function.php page as I read it could fix the problem:

    function my_pagination_rewrite() {
        add_rewrite_rule('blog/page/?([0-9]{1,})/?$', 'index.php?category_name=blog&paged=$matches[1]', 'top');
    }
    add_action('init', 'my_pagination_rewrite');
    

    Even with all these changes to supposedly fix my problem, I still can’t get to page 2 on the pagenavi navigation links. Although my blog’s home page still displays great, so I haven’t broken the display… yet!

    Can anyone see what the problem is?

    Thanks,
    SunnyOz

Viewing 4 replies - 1 through 4 (of 4 total)
  • You are mixing and matching queries.

    My blog home page has a custom query.

    This is a misleading statement. The home page of a site is the site URL with no other additions. You are referring to the Page that is designated to show your latest posts. The standard query for a Page would retrieve a single entry, but since you chose it to be the latest posts page, the query is for 9 of the latest posts.
    Since you only have 3, there is no page two for that list.
    Then you ignore the main query, and do your own query, but using the paging parameters from the main query.
    The call to wp_reset_uery is not needed, because you didn’t call query_posts (that is a good thing). See https://developer.www.ads-software.com/reference/functions/wp_reset_query/ where it says this twice. You probably should follow the advice there to use the pre_get_posts filter, so that the main query has the pagination parameters that you really want.

    Thread Starter SunnyOz

    (@sunnyoz)

    Thanks for your advice @joyously.

    I am not sure I understand all that you wrote, but I did go change the settings/reading max posts to 1 – as a test, and the pagination did work. Thanks for the tip.

    The code I displayed was used because it was suggested as a fix for the pagination problem. I have now gone back to my oy original code, and it works also. It probably is breaking WP rules also, so I will post the entire page here for you to see.

    Note: I am pulling in a widget ahead of the query that displays my blog posts. This widget has an image and some text to introduce the posts.

    <?php get_header(); ?>
    
            
        <div class="container blog-posts margin-bottom-50">
          <div class="row">
            <h1 style="margin-top: 65px;">
              <?php wp_title( '' ); ?>home
            </h1>
    		
    		    <div class="container">
    				<div class="row">
    					<div class="col-md-12 col-sm-12 col-xs-12">
    
    						<?php if ( dynamic_sidebar ( 'front-blog' ) ); ?>
    
    					</div>
    				</div>
    			</div>
    
    		<br  clear="all" />
    	
    		<?php 
    			$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;		
    			$args = array(
    					'post_type' => 'post',
    					'post_status' => 'publish',
    					'order'=> 'DESC', 
    					'orderby' => 'post_date', 
    					'category_name' => 'Recipes',
    					'posts_per_page' => 1,
    					'paged' => $paged,
    				);
    			$postslist = get_posts( $args );
    			foreach ($postslist as $post) :  setup_postdata($post); ?> 
    				<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 home-blog-list" style="float: left; display:block">				
    					<center><div class="img-responsive box-shadow shadow-effect" style=""><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
    					<h3 class="entry-title script" style="text-align: center;"><a class="entry-title-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><center>
    				</div>
    			<?php endforeach; ?>
    			<?php wp_pagenavi(); ?>
    		<br clear="all" />
          </div>
    	</div>
    	
    
    	<div class="container" style="text-align: center; margin: 0 auto;">
    		<img class="img-responsive" src="<?php bloginfo( 'template_directory' ); ?>/images/curly-divider-red.png" style="text-align: center; margin: 0 auto;">
    	</div>
    
    <br />	
    
    	<div class="container">
    		<div class="row">
    
    			<div class="col-md-12">
    				<h4 class="blog-search">Can't find what you are looking for? Use the search form below to search the site!</h4>
    				<div class="search-form-container">
    
    						<?php get_search_form(); ?>
    						
    				</div>		
    				
    		 
    			</div>
    
    		</div>
    
    <?php get_footer(); ?>
    

    Since I can now see what is being displayed on subsequent pages of the blog posts page… I see that the widget is being displayed on each page (as you would expect.) However, I think I would prefer to only show this on page 1, and if there are subsequent pages, to also display which page it is after the title.. as in something like this:

    If page one: display title, and display widget
    If not page one: display title with pagination number, and don’t display widget.

    Is this even possible? Can you help me with the code that would do both of these things?

    Thanks,
    SunnyOz

    wp_title is not for use in the page, although I can see why you might want to use it there.

    You are still doing that partial if statement for the widget area (mentioned in a different support topic). You should remove the if or make it into a real if statement.

    You didn’t change your code for the paging. All you did was change your setting from 9 to 1, but you probably don’t want to keep it at 1 for all the other category pages and search. So you still have the same problem. The paged variable is set from the main query. You need to actually change the main query by filtering pre_get_posts. That way you can use the paged variable to determine when to show the widget area.

    Thread Starter SunnyOz

    (@sunnyoz)

    Thanks again for your quick reply @joyously.

    The only if statement I have in the latest example of the code I use – is for the retrieval of my widget, not for the retrieval of my blog post entries.

    I am so confused now on what to do, especially since what I have displays properly.. I can’t tell when I have it coded wrong or right.

    Is it possible for someone to give me example code of the suggested way to do my query, so I can learn the correct way to code it, instead of me continually bumbling my way through different erroneous trials?

    Thanks,
    SunnyOz

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PageNavi plugin and custom post display – gives /page/2/ not found’ is closed to new replies.