Wordpress pagination (next_posts_link) on custom w

2019-01-15 16:48发布

问题:

Similar questions have been asked, but I cannot figure out what I miss!

I have a static page for custom type fields, similar to the regular archive or category page, but I cannot get pagination working.
If I go manually to page 2 (i.e. adding to the link .../page/2) I get the "Newer posts" link, but not on the first page for older ones! next_posts_link() just seems not to exist (no div injected or anything)

Here is my code:

  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  $query_args = array (
        'post_type' => 'bb_articoli',
        'meta_key' => 'bb_data-pubblicazione',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 2,      //for testing purposes
        'paged' => $paged,
        'meta_query' => array(
            array('key' => 'bb_fonte-pubblicazione',
                  'value' => 2,
                  'compare' => '='
                  )
        )
  );

  $query = new WP_Query($query_args);

    if ( $query->have_posts() ) :
       while ( $query->have_posts()) :
             $query->the_post();
             get_template_part( 'content' , get_post_format());
       endwhile;

     next_posts_link();
     previous_posts_link();

     else :
         get_template_part( 'content', 'none' );
 endif;

Any help is greatly appreciated. Thanks
B

Just for info: using child theme on twenty twelve

回答1:

The reason why your solution works is because you are overwriting the global $wp_query variable. A better solution would be adding $query->max_num_pages to next_posts_link().

next_posts_link('« Older Entries', $query->max_num_pages)

Where $query is the name of your newly created object. This way you preserve $wp_query.



回答2:

ok, figured it out, so want to share for future reference:

For some unknown reason next_posts_link() and previous_posts_link() only work properly if the query object is called $wp_query!

So, changing your query object accordingly gets the whole thing working:

$wp_query = new WP_Query($query_args);

if ( $wp_query->have_posts() ) :
   while ( $wp_query->have_posts()) :
         $wp_query->the_post();
           // do something
   endwhile;

 next_posts_link();
 previous_posts_link();

Works for me, but I have not tested it thoroughly. As far as I see this is not documented anywhere, surely not in the Codex. Found the answer here in comment 4 by madhavaji.

Cheers



回答3:

I had the same issue. I tried all the solutions but none of them helped me.

So for those who can't resolve this problem with the solutions above here is what I've made:

<?php
    global $wp_query, $paged;

    if( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    }else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
    }else{
        $paged = 1;
    }

    $wp_query = null;
    $args = array(
        'post_type' => array("fashion", "tv", "sport"),
        'orderby'=>'date',
        'order'=>'DESC',
        'posts_per_page' => 5,
        'paged' => $paged
    );
    $wp_query = new WP_Query();
    $wp_query->query( $args );

    while ($wp_query->have_posts()) : $wp_query->the_post();
        /* YOUR CONTENT HERE */
    endwhile;

    next_posts_link('next');
    previous_posts_link('previous');

    wp_reset_query();
?>

Important thing is to start your code with the global varriables because the next_posts_link() and previous_posts_link() functions are reading the global $paged and $wp_query values.

I hope I could help!