0

In my loop I've added:

<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
    foreach($categories as $category) {
        $output .= '<span class="post-category-info"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></span>'.$separator;
    }
echo trim($output, $separator);
}
?>

to display all the categories attached to the post here.

There is a specific category "Featured" that I want to prevent from displaying.

I've tried :

<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
    foreach($categories as $category) {
if($category !== 'Featured'){
        $output .= '<span class="post-category-info"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></span>'.$separator;}
    }
echo trim($output, $separator);
}
?>

But I guess it's a wrong approach since it's not working :)

Can anyone please help to achieve my task or at least point me in the right direction? :)

2

$category is an object containing member variables, you have to check the specific member var you want to compare.

change:

if($category !== 'Featured')

to:

if($category->name !== 'Featured')
  • Woot it worked! going to select this as a correct answer in a 5 min when the system will allow me to :) – Acidon Jun 14 '13 at 16:38
0

To take this a step further, if you wanted to exclude multiple categories try the following...

    <?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
    foreach($categories as $category) {
if($category->name !== 'category name 1') 
if($category->name !== 'category name 2')
if($category->name !== 'category name 3')
{
        $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator; }
    }
echo trim($output, $separator);
}
?>

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.