Next and previous post link in the same terms custom taxonomy

I created custom taxonomy => cat-blog

in mine custom post => blog

, cat-blog

has 4 terms

and each terms

has a list of posts belonging to thisterm

Example of terms:

- City Updates ( 4 post belong

)
- Home Tips ( 6 post belong

)
- Real Estate Guide ( 8 post belong

)
- Real Estate ( 9 post belong

)

and using this query

<?php
     $query = new WP_Query(array('posts_per_page' => 2, 'post_type' => 'blog', 'blog-cat' => get_the_term_list( $post->ID, 'blog-cat' )));
     while ($query->have_posts()) : $query->the_post();
     ?>

   <?php 
// content here
?>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
 <?php

 ?>

      

to display 2 posts in the same category, And I just want to post next

and prev

paginate, so I can navigate the rest of the post, belong to that term

.

+3


source to share


1 answer


Never change the main query for a custom query on the archive pages and the home page. The main query is already doing what you want to do. Trying to run a custom query to try and get the same result as reusing the wheel. This also causes pagination issues

Decision



  • First, remove your custom query and go back to the main loop. In your taxonomy.php, you only need the following:

    if( have_posts() ) {
       while( have_posts() ) {
         the_post();
    
         //REST OF YOUR LOOP
    
       }
    }
    
          

  • Use pre_get_posts

    in conjunction with conditional tags if you need to modify the main query. For example, if you need 2 posts per page in taxonomy page, do the following functions in functions.php

    function so26499451_custom_ppp( $query ) {
        if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '2' );
        }
    }
    add_action( 'pre_get_posts', 'so26499451_custom_ppp' );
    
          

Now you can look as usual without any problems. You will now see two entries from a specific term that you clicked on in your taxonomy.php page.

+1


source







All Articles