Skip to main content
deleted 236 characters in body
Source Link

After hours of unfruitful Googling, I am asking for your help on this issue.

My objective is to display only 1 category with get_the_category if the category ID matches the ID or slug set in the get_the_category function. Basically I'm asking for something like this: let's say there's a category titled "important". I want to exclude all other categories assigned to the post and only show "important" if the post is within that category. If the post isn't in important, nothing should be displayed. This should be too hard, but I could figure it out.

Here's the closest thing I found to this, but it only displays the first category for the post, no option for manual selection by ID or slug:

<?php 
$category = get_the_category(); 
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

A nice touch would be to also have &middot; after the specified category if the post matches the category (for example, if post is in category "important" then it would display as "important [end of category url here] &middot;".

Thank you for your contributions.

After hours of unfruitful Googling, I am asking for your help on this issue.

My objective is to display only 1 category with get_the_category if the category ID matches the ID or slug set in the get_the_category function. Basically I'm asking for something like this: let's say there's a category titled "important". I want to exclude all other categories assigned to the post and only show "important" if the post is within that category. If the post isn't in important, nothing should be displayed. This should be too hard, but I could figure it out.

Here's the closest thing I found to this, but it only displays the first category for the post, no option for manual selection by ID or slug:

<?php 
$category = get_the_category(); 
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

A nice touch would be to also have &middot; after the specified category if the post matches the category (for example, if post is in category "important" then it would display as "important [end of category url here] &middot;".

Thank you for your contributions.

After hours of unfruitful Googling, I am asking for your help on this issue.

My objective is to display only 1 category with get_the_category if the category ID matches the ID or slug set in the get_the_category function. Basically I'm asking for something like this: let's say there's a category titled "important". I want to exclude all other categories assigned to the post and only show "important" if the post is within that category. If the post isn't in important, nothing should be displayed. This should be too hard, but I could figure it out.

Here's the closest thing I found to this, but it only displays the first category for the post, no option for manual selection by ID or slug:

<?php 
$category = get_the_category(); 
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

Thank you for your contributions.

Source Link

Display 1 category only with get_the_category (by ID or slug)

After hours of unfruitful Googling, I am asking for your help on this issue.

My objective is to display only 1 category with get_the_category if the category ID matches the ID or slug set in the get_the_category function. Basically I'm asking for something like this: let's say there's a category titled "important". I want to exclude all other categories assigned to the post and only show "important" if the post is within that category. If the post isn't in important, nothing should be displayed. This should be too hard, but I could figure it out.

Here's the closest thing I found to this, but it only displays the first category for the post, no option for manual selection by ID or slug:

<?php 
$category = get_the_category(); 
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

A nice touch would be to also have &middot; after the specified category if the post matches the category (for example, if post is in category "important" then it would display as "important [end of category url here] &middot;".

Thank you for your contributions.