0

I use categories to order my posts into timelines. So for example, Jennifer Lopez

There are no subcategories. All categories are on one level.

The top post, "Wants Gomez" actually has two categories: Jennifer Lopez and Selina Gomez. I'd like to display only the categories that are not in the current category, so in this case I'd like to see "Selina Gomez" displayed after the date.

Some posts may have three or more categories. For example, if a post had "George Bush, Dick Cheney, Barack Obama" as categories, when I look at the Barack Obama category I would see only George Bush, Dick Cheney displayed on the post, but if I looked at the George Bush category I would see Obama and Cheney.

I need the category name to link to the category page i.e. "Barack Obama" text will link to the Barack Obama category page.

  • I didn't even know how to start. But the answer below has given me some pointers. Thanks. – Mark Devlin Jun 27 '14 at 16:12
0

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;
?>
  • Excellent. This works great. – Mark Devlin Jul 1 '14 at 6:49
  • Please accept my answer then by ticking the check mark next to my answer right below the up/down arrows :-) – Pieter Goosen Jul 1 '14 at 6:59
0

You can try to get the category of current category page, using

single_cat_title( $prefix, $display );

about which, you can read in codex here

this should be used outside the loop.

After that, the place where you querying for getting categories to show in your pages, you can exclude the current category from the array provided to you by

wp_get_object_terms( $object_ids, $taxonomies, $args )

and then it will no longer display the category page's current category.

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.