I don’t know if you solved this yet, but if you did, it would be great to see how you approached it.
Here is my approach:
The first thing would be is to retrieve the name of the category page you are currently viewing. You would only want to do this on a category page only as you said. You can use get_queried_object
to get the category name, all inside the conditional tag is_category()
.
if(is_category()) { // only run this on category page
$cc = get_queried_object();
$exclude = $cc->cat_name; //get the name of current category page
}
Next would be to retrieve the categories a post belongs to with get_the_category
. From that list, you will then exclude the current category, also only if you are on a category page.
<?php
if(is_category()):// only run this on category page
$cc = get_queried_object();
$exclude = $cc->cat_name; //get the name of current category page
endif;
$categories = get_the_category();
foreach($categories as $category) :
if(is_category() && $category->name==$exclude)
continue; //skip current category from being displayed only on category pages
$category_link = get_category_link( $category );
echo '<li><a href="'.$category_link.'">'.$category->cat_name.'</a></li>';
endforeach;
?>