1

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 Answers 2

4

$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')
1
  • Woot it worked! going to select this as a correct answer in a 5 min when the system will allow me to :)
    – Acidon
    Commented Jun 14, 2013 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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.