I want to get the link to a category (specified by id) but with a paged value (e.g. page 3 of category id 4).

I was looking into get_category_link()Codex, but it only returns the link to the first page of the category.

Is there a build-in function that I can make use of to get the link to a category page that reflects the blogs URL-layout settings (?paged=3/ /page/3)?

Edit: I added my solution as an answer.

  • Can you clarify what you mean, categories aren't paged, archives are... are you referring to category archive paging and linking to specific pages in that category's archive?
    – t31os
    Aug 16 '11 at 23:14
  • I mean that get_category_link displays the first page only, as written in the question. This might be called category archive, right, so categories are paged (in archives). And that's what I mean :) - right now I'm adding stuff to the link based on what permalinks related getter functions of $wp_query return.
    – hakre
    Aug 17 '11 at 0:25
  • 2
    maybe add_query_arg?
    – Bainternet
    Aug 17 '11 at 6:11
  • add_query_arg might be helpful to streamline if permalinks are turned off, will check. I'll add my code so far.
    – hakre
    Aug 17 '11 at 7:38
  • add_query_arg would be the way to go imo.. you'd then not need the strpos line.
    – t31os
    Aug 17 '11 at 9:11

This the code I have so far which looks like it does the job, I still wonder if there is a API function for the job:

 * @param (int) $category_id
 * @param (int) $pagenum
 * @return string
function get_category_paged_link($category_id, $pagenum)
    global $wp_rewrite;

    $link = get_category_link($category_id);

    if ($wp_rewrite->using_permalinks() || $wp_rewrite->using_index_permalinks())
        $link = sprintf
            rtrim($link, '/'),
        if (false === strpos($link, '?'))
            $link .= '?';
            $link .= '&';
        $link .= sprintf('paged=%d', $pagenum);

    return $link;

I've found that get_term_link()(or similarly get_category_link) will output a pretty URL depending on where it's called, when called too early(i was testing inside the theme functions file initially) it will just output a regular query var style URL, eg. http://example.com/?cat=5, where as when called later on(i tested in the theme's footer), you'll get a permalink style URL, eg. http://example.com/category/nicename.

Clearly those functions rely on something that gets included later in the load sequence(at least after the theme's functions file), i can't pinpoint why they behave this way, but that's my initial observation, which also means my previous comment regarding there not being an internal function was incorrect.

  • So in short, depending on where and when you call the functions you may get a pretty url or a regular query string url.
    – t31os
    Aug 17 '11 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.