简体   繁体   English

分页不适用于自定义帖子类型的类别

[英]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. 当我对自定义帖子类型product使用分页时,它可以正常工作,但不适用于自定义帖子类型的类别。 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. 分页适用于此http://localhost/wordpress/products/page/2/ ,而不适用于此http://localhost/wordpress/products/landscape/page/2/它始终显示第一页内容。 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' -用“ paged”替换“ page”参数

-Replace 'taxonomy' with 'tax_query'. -将“分类法”替换为“ 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: 在您的问题中,您使用$cat_name1了条款,然后请使用以下代码:

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: 请使用以下代码替换wp_pagenavi()函数:

$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
) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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