How do I get the category link for a given post in the loop?
I can get an array of the categories using
get_the_category()
in the loop, then I can extract the category name and nicename from each array item. However, I'd like to be able to get a link for the archive for each post category that takes into account permalink settings for the blog. Anyone know how I can do this?
I need to format different items in the list of post categories in different ways, or I'd just use the standard template the_category() function.
Cheers,
a|x
Just realised my question makes no sense, since permalinks relate only to single posts. What I need to do actually is get the path for the category archive. I think I know how to do that, at least for parent and first-child categories.
a|x
In case anyone is interested, this seems to work. Dunno if it's the most efficient way of doing it, mind.
$thecats = '';
$cats = get_the_category();
foreach( $cats as $cat ) {
$catlink = '<a href="' . get_bloginfo( 'url' ) . '/category/';
if( $cat->category_parent ) $catlink .= get_category( $cat->category_parent )->slug . '/';
$catlink .= $cat->slug . '/" title="View all posts in ' . $cat->cat_name . '" rel="category tag"';
if( !strcmp( $cat->category_nicename, 'hackney' ) ) $catlink .= 'class="ell_hackney_color"';
if( !strcmp( $cat->category_nicename, 'tower-hamlets' ) ) $catlink .= 'class="ell_thamlets_color"';
if( !strcmp( $cat->category_nicename, 'lewisham' ) ) $catlink .= 'class="ell_lewisham_color"';
if( !strcmp( $cat->category_nicename, 'croydon' ) ) $catlink .= 'class="ell_croydon_color"';
$catlink .= '>' . $cat->cat_name . '</a>, ';
$thecats .= $catlink;
}
// Trim trailing comma and whitespace and echo
echo substr($thecats,0,-2);
Cheers,
a|x