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

时间:2016-10-19 06:18:02

标签: php wordpress pagination

当我为自定义帖子类型product使用分页时,它的工作正常,但它不适用于自定义帖子类型的类别。对于前分页为此http://localhost/wordpress/products/page/2/工作而不是此http://localhost/wordpress/products/landscape/page/2/,它始终显示第一页内容。怎么解决这个?以下是我的代码。

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

1 个答案:

答案 0 :(得分:2)

试试这个:

-Replace&#39; page&#39;参与&#39; paged&#39;

- 取代分类&#39;与&#39; tax_query&#39;。

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

在您的问题中,您已使用$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 );

分页:

请使用以下代码替换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
) );