简体   繁体   中英

pagination not working for category of custom post type

When I use pagination for custom post type product its working fine but its not working for the categories of custom post type. for ex. pagination working for this http://localhost/wordpress/products/page/2/ and not for this http://localhost/wordpress/products/landscape/page/2/ its always showing the first page content. How to solve this? given below is my code.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'taxonomy' =>'product_cat','term' => $cat_name1,'orderby'=>'post_date','page'=>$paged );
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) : while ($wp_query->have_posts()): $wp_query->the_post();

<div class="product_list">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $wp_query ) );//plugin code ?>
<?php else : ?>
<!-- No posts found -->
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo "No Products found for this categoy!." ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>

Try this :

-Replace 'page' arguments with 'paged'

-Replace 'taxonomy' with 'tax_query'.

if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$taxonomy_terms = get_terms( $taxonomy, array(
    'hide_empty' => 0,
    'fields' => 'ids'
) );


$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'id',
            'terms' => $taxonomy_terms,
        ),
    ),'orderby'=>'post_date','paged'=>$paged );

In your question, you have used $cat_name1 for terms listing then please use following code:

if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';

$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'id',
            'terms' => $cat_name1,
        ),
    ),'orderby'=>'post_date','paged'=>$paged );

Pagination :

please replace wp_pagenavi() function with following code:

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '/page/%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM