x to y of z search results II

in code


The code snippet I posted last August hasn’t worked too well:

  • It only works for the index.php loop of all posts.
  • All other loops (search, taxonomy, archive, etc.) have weird counts.

I changed it to use the global wp_query variable to determine post count, with an addendum for private posts if I am logged in. get_loop_type is a shorthand function I wrote a while ago.

function archive_page_count($echo = false, $page_num = null, $total_results = null) {
    global $wp_query;

    if (is_null($total_results)) {
        $total_results = $wp_query->found_posts;
    }

    if (is_null($page_num)) {
        $page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
    }

    $posts_per_page = get_option('posts_per_page');
    $total_pages = ceil($total_results / $posts_per_page);
    $page_count = sprintf(__('Page %s of %s', TTD), $page_num, $total_pages);

    if (!$echo) {
        return $page_count;
    }

    printf($page_count);
}


A Summer of Running

in running

Achill Half Marathon

in running


Your email address will not be published. Required fields are marked *