I’m having problems with pagination in a query with custom taxonomy. When I go to the second page this return me a 404 error page.
This is my code (run in index.php):
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'destacado' ),
'operator' => 'NOT IN',
),
),
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="normal">
<a class="imagen" href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<a class="nombre" href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
<p class="marca"><?php $terms = get_the_terms( $loop->post->ID, 'marca' ); foreach($terms as $term) { echo $term->name; } ?></p>
<p class="precio"><?php echo $product->get_price_html(); ?></p>
<span class="agregar"><?php woocommerce_template_loop_add_to_cart(); ?></span>
</li>
<?php endwhile; ?>
</ul>
<?php post_navigation(); ?>
<?php } else { echo __( 'No products found' ); }?>
<?php wp_reset_postdata();?>
The post_navigation() code in functions.php is:
function post_navigation() {
echo '<div class="navigation">';
echo ' <div class="next-posts">'.get_next_posts_link('« Older Entries').'</div>';
echo ' <div class="prev-posts">'.get_previous_posts_link('Newer Entries »').'</div>';
echo '</div>';
}
Thanks!
try this