• Hello,

    I’m a beginer wordpress developer, i’ve been using wordpress since 2 month ago, and im having difficulty on pagination… i’m using custom template and all the post generated using query_post on my page i used 3 query_posts to generate different content on 1 page…

    my problem is, on this page, i want some pagination but i always ended up on 404 when im on the fourth page (not second or third page).. i will show u my code, i also put the total post on each query_post that i used..

    my first query_posts

    <?php
    				$args = array(
    						'post_type' => 'slide',
    						'order' => 'ASC'
    						);
    				query_posts($args);
    				if(have_posts()) : while(have_posts()) : the_post();
    			?>
    			<li>
    				<?php the_post_thumbnail(); ?>
    			</li>
    			<?php
    				endwhile; endif;
    				wp_reset_query();
    
    				// OUTPUT 4 POST
    			?>

    my second query_posts

    <?php
    					$args = array(
    							'post_type' => 'home',
    							'meta_query' => array(
    									array(
    											'key' => 'type',
    											'value' => 'Main Title Home'  //im using CUSTOM POST TYPE ON THIS ONE...
    									)
    							)
    						);
    					query_posts($args);
    					if(have_posts()) : the_post();
    				?>
                	<h1 class="blue"><?php the_title(); ?></h1>
    				<?php the_content(); ?>
    				<?php
    					endif;
    					wp_reset_query();
    
    					// OUTPUT 1 POST
    				?>

    my third query_post (this one is the post that i want to add pagination)

    <?php
    					$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    					$args = array(
    							'posts_per_page' => 1,
    							'category_name' => 'gallery',
    							'paged' => get_query_var('paged'),
    							'order' => 'DESC'
    						);
    					query_posts($args);
    
    					if(have_posts()) : while(have_posts()) : the_post();
    					?>
    					<!-- START LOOP -->
                    	<div class="kolom-blog">
                        	<div class="kolom-blog-tanggal">
                            <?php the_time('j'); ?>
                            <span><?php the_time('F'); ?></span>
                            </div>
                        	<div class="kolom-blog-judul">
                            <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                            </div>
                            <div class="clear"></div>
                            <div class="kolom-blog-gambar">
                            <?php the_post_thumbnail(); ?>
                            </div>
                            <div class="kolom-blog-teks">
    						<?php the_excerpt(); ?>
                            </div>
                        </div>
    					<?php
    						endwhile; endif;
    						wp_reset_query();
    
    						// OUTPUT 8 POST
    					?>
    
    					<!-- END LOOP -->

    and here my pagination function:

    <?php
    
    								global $wp_query;
    
    								$big = 999999999; // need an unlikely integer
    
    								echo paginate_links( array(
    									'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    									'format' => '?paged=%#%',
    									'current' => max( 1, get_query_var('paged') ),
    									'total' => $wp_query->max_num_pages,
    									'prev_text'    => __('PREVIOUS'),
    									'next_text'    => __('NEXT')
    								) );
    
    							?>

    i’ve already tried to refresh my permalink structure, set my default post per page to 1 from backend, using next_posts_link() & previous_posts_link() but they always redirect me to 404 when im on the fourth page and so on…

    i’ve also heard that using query_post can damage pagination, are ay other way to safely implement pagination using query_posts??

    Thank You Very Much
    and sorry for my bad english…

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Is this a page template?
    http://codex.wordpress.org/Page_Templates
    If so don’t use query_posts but use new WP_Query for all your loops.
    http://codex.wordpress.org/index.php?title=Class_Reference/WP_Query
    Reset the post data after all your (new WP_Query) loops using wp_reset_postdata();
    http://codex.wordpress.org/Function_Reference/wp_reset_postdata

    The only thing you have to do with the pagination function is setting the “total” to the total posts of your last loop.
    Example last loop (third query):

    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $args = array(
    	'posts_per_page' => 1,
    	'category_name' => 'gallery',
    	'paged' => $paged,
    	'order' => 'DESC'
    );
    
    $query_three = new WP_Query( $args );
    
    if ( $query_three->have_posts() ) :  while ( $query_three->have_posts() ) : $query_three->the_post();
    ?>
    
    					<!-- START LOOP -->
                    	<div class="kolom-blog">
                        	<div class="kolom-blog-tanggal">
                            <?php the_time( 'j' ); ?>
                            <span><?php the_time( 'F' ); ?></span>
                            </div>
                        	<div class="kolom-blog-judul">
                            <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                            </div>
                            <div class="clear"></div>
                            <div class="kolom-blog-gambar">
                            <?php the_post_thumbnail(); ?>
                            </div>
                            <div class="kolom-blog-teks">
    						<?php the_excerpt(); ?>
                            </div>
                        </div>
    <?php endwhile; ?>
    
    <?php
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var( 'paged' ) ),
    		// set total to total of last loop
    		'total' => $query_three->max_num_pages,
    		'prev_text'    => __( 'PREVIOUS' ),
    		'next_text'    => __( 'NEXT' )
    	) );
    
    ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    
    <!-- END LOOP -->

    Thread Starter victorfliks

    (@victorfliks)

    Thank you for the reply,

    yes, i’m using page templates, by creating template.php and set template name etc..

    ok, i will test the code on my office tomorrow, i will reply you whenever it’s work or not 😀

    sorry for asking, but
    page template are the only method i know to make pages, are there any other (save) method to create a page.. Or should i stick to page template method on my other wordpress project..

    Thank You Very Much…

    Thread Starter victorfliks

    (@victorfliks)

    ow yeah, i forgot to tell, that this code is in index.php i dont using page templates on this one…..

    Thread Starter victorfliks

    (@victorfliks)

    hi keesiemeijer, i’ve used your method and change all the query_posts to wp_query, i named it query_one, query_two and query_three…

    the number of page display is correct, they show 8 pages on pagination (based on: how many post/posts_per_page), but the conflict still appear, when i choose page number 5, they still redirect me to 404…

    so in my opinion, my wordpress redirect me to 404 because they don’t create page ‘www.example.com/page/5’ an so on… even though i’ve created the permalink for page above 4….

    my permalink structure, is http://www.example.com/postname&#8230;

    Thank You Very Much..

    Thread Starter victorfliks

    (@victorfliks)

    i’ve found a solution

    i’ve added this into function.php

    function my_post_queries( $query ) {
    	  // do not alter the query on wp-admin pages and only alter it if it's the main query
    	  if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home and category pages 
    
    		if(is_home()){
    		  $query->set('posts_per_page', 1); // changed from 3 to 1...
    		}
    
    		if(is_category()){
    		  $query->set('posts_per_page', 3);
    		}
    
    	  }
    	}
    	add_action( 'pre_get_posts', 'my_post_queries' );

    now i can switch into diffrent page without error, but there are some downside..

    because i’m using it on index.php, if i set the $query->set('posts_per_page', 1); i can open page up to ‘www.example.com/page/12’ that means that in index.php i’ve outputed 12 post…

    so for my solution…

    first if anyone using pagination on index.php add this on function.php

    function my_post_queries( $query ) {
    	  // do not alter the query on wp-admin pages and only alter it if it's the main query
    	  if (!is_admin() && $query->is_main_query()){
    
    		// alter the query for the home pages 
    
    		if(is_home()){
                      $query->set('cat','2') // this means select all post from category ID 2...
    		  $query->set('posts_per_page', 1); // this means set maximum post outputted on index.php
    
                      // basic calculation how many pages generated: if on category ID 2  you have 4 post, that means 4 / 1(from posts_per_page) equal to 4 pages generated...
    
    		}
    
    	  }
    	}
    	add_action( 'pre_get_posts', 'my_post_queries' );

    on index.php use the keesiemeijer method by using wp_query…

    and thats all..

    hope this is the best method for me, and fell free if you have another suggestion regarding pagination…

    thank you so much for keesiemeijer for helping me…. 😀

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pagination Error 404 on specific page’ is closed to new replies.