Reputation: 126
Trying to paginate custom post types by custom taxonomy terms and default posts by category but it does not working. I'm novice to WordPress, I did lots of googling and didn't find a detailed solution to solve my issue.
If anyone knows please share your answer, it's really appreciated.
Note: I created a custom theme.
Here is my taxonomy-product_category.php page code:
<?php get_header();?>
<div class="container-fluid pages">
<div class="container" style="margin-top: 10%;">
<div class="row">
<div class="col-sm-8">
<div class="panel panel-default">
<?php $term = get_queried_object();
$taxonomy = get_taxonomy($term->taxonomy);
?>
<div class="panel-heading"><h4><?php echo 'محصولات : '.$term- >name;?></h4></div>
<div class="panel-body">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type'=>'my_product',
'taxonomy'=>$taxonomy->name,
'posts_per_page'=> 1,
'term'=>$term->slug,
'paged'=>$paged);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()): $query->the_post(); ?>
<div class="thumbnail">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('featured');
} ?>
<div class="caption caption-content">
<h3 class="product-name"><?php the_title(); ?></h3>
<p class="text-muted">
<!-- <strong>نویسنده:</strong> <?php //the_author();?> -->
تاریخ: <?php the_date(); ?> </p>
<p> <?php the_excerpt(); ?> </p>
<div>
<p class="price-box">
<i class="fa fa- circle"> </i>قیمت:
<?php if (the_field('price') == '') {
// echo "00.00";
} else {
the_field('price');
} ?>
</p>
</div>
</div>
<hr>
</div>
<?php endwhile;
}
else {
echo '<h3>هیچ موردی درین بخش یافت نشد.</h3>';
}
?>
<!-- pagination here -->
<p>
<nav>
<?php if ( function_exists( 'custom_pagination' ) ) {
custom_pagination($custom_query- >max_num_pages,"", $paged);
} ?>
</nav>
<?php wp_reset_postdata(); ?>
</p>
</div>
</div>
</div>
<?php get_sidebar(); ?>
</div>
</div>
</div>
<?php get_footer();?>
When I click on next page it redirects me to index page where the url is still: http://localhost/goldsotre/product_category/ring/page/2/ Custom_pagination function:
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
Upvotes: 0
Views: 2227
Reputation: 2471
Chance is posts_per_page
got overridden by Blog pages show at most value.
I assume you're using the paginate_link()
function, try this in functions.php
:
add_action('pre_get_posts', function($query)
{
if ($query->is_tax('product_category')) {
$query->set('posts_per_page', 1);
}
});
Make sure product_category
is the taxonomy of the archive page. This article maybe helpful for you.
Upvotes: 2
Reputation: 126
If I use below function instead of that I can see the pagination links but not working, when clicking on the next link, I get 404 page.
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages ) );
Upvotes: 1