Good day! The problem is this: in the template category(the archive) the pagination is not working, when you click on page 2 of the 404 error. Please help do not understand how to solve it, already all head broke
My loop:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$arg = array(
'cat' => get_queried_object_id(),
'post_type'=>'post',
'posts_per_page'=>9,
//'order'=>'desc',
'paged' => $paged,
);
$query = new WP_query($arg);
if($query->have_posts()) : ?>
<section class="blog">
<?php
echo '<div class="row">';
$i=0;
$formcreated=false;
while( $query->have_posts() ) :
$query->the_post();
// display post
endwhile;
wp_reset_postdata();
endif;
?>
<div class="pagination">
<?php
if (function_exists('custom_pagination')) {
custom_pagination($query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
</div>
And my custom pagination:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_category()){
$query->set('posts_per_page', 1);
$query->set('post_type','product');
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$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 $paginate_links;
echo "</nav>";
}
}
Since this has come up in two different forums lately, I am answering this.
If you use custom pagination, such as the one you're using which appears to come from http://callmenick.com/post/custom-wordpress-loop-with-pagination but it happens with Genesis child themes too because the parent numbered pagination is what people what.
Why do you get a 404 page? The Custom pagination in the callmenick.com and Genesis (genesis_posts_nav) is for the main query and so if your pagination for your different query is under the posts per page in your reading settings (which is set for the main query), then you will get a 404 on page 2.
You don't see this question a lot because many just build the pagination for that loop instead of re-using it from the functions.php file or a parent theme. You can learn that here: https://codex.wordpress.org/Function_Reference/paginate_links
Let's start from the top and whenever you code, turn on debug in wp-config.php
A basic custom loop in my cpt archive.
archive-product.php
In your functions.php file:
Learn about conditionals. https://codex.wordpress.org/Conditional_Tags https://codex.wordpress.org/Function_Reference/is_post_type_archive