• Hi everybody,

    I am developing a theme locally, so I don’t have a link right now.
    The problem is that my pagination shows, also if not needed: it sends to an empty page number 2. Anyone has ideas about this error?

    I paste the code

    <ul class="normal-articles cf">
    
    			<?php
    			$articoli_taccuino = new WP_Query( 'cat=taccuino&posts_per_page=4&paged='.$paged );
    			while($articoli_taccuino->have_posts()) : $articoli_taccuino->the_post();
    			?>
    
    			<li class="num-2">
    				<article class="cf">
    
    					<figure>
    					<a>"><?php if( has_post_thumbnail()) { the_post_thumbnail('normal'); }?></a>
    					</figure>
    
    					<section class="text">
    					<h2><a>"><?php the_title(); ?></a></h2>
    					<?php the_excerpt(); ?>
    					<a>">leggi di più…</a>
    					</section>
    
    				</article>
    
    			<?php
    			endwhile;
    			wp_reset_postdata();
    			?>
    
    		<!-- PAGINAZIONE -->
    		<div class="pagination">
    		<?php
    		global $wp_query;
    		$infinito = 999999999;
    		echo paginate_links( array(
    		'base' => str_replace( $infinito, '%#%', get_pagenum_link( $infinito ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var('paged') ),
    		'total' => $wp_query->max_num_pages,
    		'prev_text' => 'Precedenti |',
    		'next_text' => '| Successivi'
    		));
    		?>
    		</div>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with wp_reset_postdata(); after the pagination.
    And change this:

    <?php
    		global $wp_query;
    		$infinito = 999999999;
    		echo paginate_links( array(
    		'base' => str_replace( $infinito, '%#%', get_pagenum_link( $infinito ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var('paged') ),
    		'total' => $wp_query->max_num_pages,
    		'prev_text' => 'Precedenti |',
    		'next_text' => '| Successivi'
    		));
    		?>

    to this:

    <?php
    		$infinito = 999999999;
    		echo paginate_links( array(
    		'base' => str_replace( $infinito, '%#%', get_pagenum_link( $infinito ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var('paged') ),
    		'total' => $articoli_taccuino->max_num_pages,
    		'prev_text' => 'Precedenti |',
    		'next_text' => '| Successivi'
    		));
    		?>

    Thread Starter mattiafrigeri

    (@mattiafrigeri)

    Hi,

    moving the “reset”, as you suggest, after the pagination doesn’t make sense, because the loop is structured in a certain way and it would break. I see now I pasted wrong code, I’ll put the correct one.
    Lastly, changing the second part doesn’t seem to work either: it appears correct first, but then when I go to page 2, I see the “white screen of death”.

    Thanks anyway.

    This is the correct code of the page:

    <ul class="normal-articles cf">
    
    			<?php
    			$articoli_taccuino = new WP_Query( 'cat=taccuino&posts_per_page=2&paged='.$paged );
    			while($articoli_taccuino->have_posts()) : $articoli_taccuino->the_post();
    			?>
    
    <li>
    				<article class="cf">
    
    					<figure>
    					<a>"><?php if( has_post_thumbnail()) { the_post_thumbnail('normal'); }?></a>
    					</figure>
    
    					<section class="text">
    					<h2><a>"><?php the_title(); ?></a></h2>
    					<?php the_excerpt(); ?>
    					<a>">leggi di più…</a>
    					</section>
    
    				</article>
    			</li>
    			<?php
    			endwhile;
    			wp_reset_postdata();
    			?>
    
    		<!-- PAGINAZIONE -->
    		<div class="pagination">
    		<?php
    		global $wp_query;
    		$infinito = 999999999;
    		echo paginate_links( array(
    		'base' => str_replace( $infinito, '%#%', get_pagenum_link( $infinito ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var('paged') ),
    		'total' => $wp_query->max_num_pages,
    		'prev_text' => 'Precedenti |',
    		'next_text' => '| Successivi'
    		));
    		?>
    		</div>
    Moderator keesiemeijer

    (@keesiemeijer)

    moving the “reset”, as you suggest, after the pagination doesn’t make sense, because the loop is structured in a certain way and it would break.

    Without seeing the full template I don’t know what you mean by this.

    wp_reset_postdata() restores the $post global to the current post in the main query. The pagination that comes after the reset will now use the main query ($wp_query) again instead of the $articoli_taccuino query.

    Try and change this:

    'total' => $wp_query->max_num_pages,

    to this:

    'total' => $articoli_taccuino->max_num_pages,

    Thread Starter mattiafrigeri

    (@mattiafrigeri)

    Ok:

    1. moving the:

    wp_reset_postdata()

    after the pagination has no effects.

    2. changing

    'total' => $articoli_taccuino->max_num_pages,

    works as before, but then when I click to page 2, I see the white screen.

    3. I also deleted the

    global $wp_query;

    as you suggested the first time, but it doesn’t change anything.

    This is really strange as other my blogs use the same structure, and only this gives me porblems…

    Moderator keesiemeijer

    (@keesiemeijer)

    Did you set the $paged parameter before the query

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php
    			$articoli_taccuino = new WP_Query( 'cat=taccuino&posts_per_page=2&paged='.$paged );
    			while($articoli_taccuino->have_posts()) : $articoli_taccuino->the_post();
    			?>

    works as before, but then when I click to page 2, I see the white screen.

    Is this a 404 (posts not found) or an entire blank screen?
    On what theme themplate are you using this?

    Thread Starter mattiafrigeri

    (@mattiafrigeri)

    Dear Keesiemeijer,

    first thanks for your precious help.
    Then:

    1) I didn’t set the $paged parameter, but I have never set that in the others blogs too, and it works fine there. Do you need it with wp_query()? Anyway, I tried to do it, but nothing changed. You can see that I am not full aware of how all this works… ;(

    2) It’s not a 404, it’s the white screen of death..

    3) I am using this on a theme made by me, and the template file is called category-taccuino.php. As well, other my blogs use similar templates, and it works. Sob.

    Moderator keesiemeijer

    (@keesiemeijer)

    Maybe your theme needs a 404 template to let it not be the white screen of death when there are no posts found (not sure about this):
    https://codex.www.ads-software.com/Creating_an_Error_404_Page

    The ‘cat’ parameter uses category IDs, but try querying without the ‘cat’ parameter as this template will only be used for the taccuino category posts anyway:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php
    			$articoli_taccuino = new WP_Query( 'posts_per_page=2&paged='.$paged );
    			while($articoli_taccuino->have_posts()) : $articoli_taccuino->the_post();
    			?>

    https://codex.www.ads-software.com/Function_Reference/WP_Query#Category_Parameters

    As this is a category page there is a more reliable way to query the main loop from your theme’s functions.php. Remove the query and use a normal loop in your template:

    <?php
    // normal loop
    while ( have_posts() ) : the_post();
    ?>

    And in your functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category('taccuino')){
          $query->set('posts_per_page', 2);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    https://www.billerickson.net/customize-the-wordpress-query/
    https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
    https://codex.www.ads-software.com/Pagination#Removing_query_posts_from_the_main_loop
    https://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

    Thread Starter mattiafrigeri

    (@mattiafrigeri)

    I really don’t know why, but JUST inserting your code (this):

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category('taccuino')){
          $query->set('posts_per_page', 2);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    in functions.php, did the trick! Without changing anything else.
    Do you know why???? (!!!)
    I don’t like it ;(

    but thanks

    Moderator keesiemeijer

    (@keesiemeijer)

    What’s not to like, querying with ‘pre_get_posts’ you modify the main query before WordPress gets the posts from the database (one less database query).
    https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
    new WP_Query() is usually used for secondary loops, not the main loop. However you can also use it for the main loop. Do you have multiple loops (or queries) on the category-taccuino.php template?

    Did you remove the new WP_Query() from your template and used a normal loop?

    Thread Starter mattiafrigeri

    (@mattiafrigeri)

    Hi kees, sorry for the delay but I was away ;(

    I like what you did and you are evidently aware, what I don’t like is me not understanding. ??

    I have a single loop on that category (taccuino), and used a new WP_Query() just because I know how to use it (or at least I thought).

    I didn’t remove the WP_Query nor modified anything in that page, just added the code in functions.php! This is what I don’t like, this strange fact that it only worked but I couldn’t understand why…

    You’re the king.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Pagination with wp_query()’ is closed to new replies.