Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

up vote 1 down vote accepted

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
        (
            '%s/%s/%d/',
            rtrim($link, '/'),
            $wp_rewrite->pagination_base,
            $pagenum
        );
    }
    else
    {
        if (false === strpos($link, '?'))
            $link .= '?';
        else
            $link .= '&';
        $link .= sprintf('paged=%d', $pagenum);
    }

    return $link;
}
share|improve this answer

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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