pagination – get_query_var(‘paged’) return 0 in custom category page

Question

I want to make a pagination for my category page (category is related to custom post type)
But I can’t get current page because it returns 0.

$term = get_queried_object();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo get_query_var('paged'); // Output is 0 (Error Here)

$args = array(
    "post_type" => "readings",
    "posts_per_page" => 2,
    "cat" => $term->term_id,
    "paged" => $paged
);

$posts = new WP_Query($args);

and in functions.php

function pagination(){
    global $wp_query;
    $all_pages = $wp_query->posts->max_num_pages;

    $current_page = max(1, $wp_query->posts->query_vars["paged"]);


    if($all_pages > 1){
        return paginate_links(array(
            "base" => get_pagenum_link() . "%_%",
            "format" => "/page/%#%",
            "current" => $current_page,
            "total" => $all_pages,
            "prev_text" => '<i class="fa-solid fa-angle-left"></i>',
            "next_text" => '<i class="fa-solid fa-angle-right"></i>',
        ));
    }

}

0
Marco Talaat 4 months 2022-03-30T13:42:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse