WordPress subcategory archive pagination in custom layout

Question

I have blog with categories and sub-categories and use category.php for archive of main category posts, and added a custom layout for subcategory archive layout as subcategory.php (with a function).

In this subcategory archive page, I need a numbered pagination but all I did it returns to 404 Not Found, here my code :

function for custom layout (in fucntions.php):

function wpd_subcategory_template($template) {
    $cat = get_queried_object();
    if (0 < $cat->category_parent)
        $template = locate_template('subcategory.php');

    return $template;
}

add_filter('category_template', 'wpd_subcategory_template');

then in subcategory.php :

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_news = array(
    'paged'          => $paged,
    'numberposts'    => -1,
    'posts_per_page' => 2,
    'orderby'        => 'date',
    'order'          => 'DESC',
    'post_status'    => 'publish',
    'post_type'      => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'featured',
            'hide_empty' => true,
        ),
    ),
);
$sub_cat_news = new WP_Query($args_news);

while ($sub_cat_news->have_posts()) : $sub_cat_news->the_post();
    get_template_part('....');
endwhile;

    $total_pages = $sub_cat_news->max_num_pages;

    if ($total_pages > 1) {
        $current_page = max(1, get_query_var('paged'));
        define('CURRENT_PAGE', $current_page);

        echo paginate_links(array(
            'base'      => get_pagenum_link(1) . '%_%',
            'format'    => 'page/%#%',
            'current'   => $current_page,
            'total'     => $total_pages,
            'prev_text' => __('<svg></svg>'),
            'next_text' => __('<svg></svg>'),
            'type'      => 'list',
            ));
    }

    wp_reset_postdata();
  • the query and tax_query works fine and I have all post of this subcategory. I have 10 posts in this subcategory and pagination creates on 2 pages it redirects here :
    domain.com/category/maincategory/subcategory/page/2 but error 404

any idea, thanks !

0
GMedija 2 months 0 Answers 16 views 0

Leave an answer