Forums

[resolved] Category Links (3 posts)

  1. toneburst
    Member
    Posted 1 year ago #

    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

  2. toneburst
    Member
    Posted 1 year ago #

    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

  3. toneburst
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic