List categories of a post but exclude a specific one

35 Views Asked by At

I am trying to list posts with the categories they are associated but excluding a specific one. I tried the following code but the category is still showed.

<?php if ($the_query->have_posts()) : ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
        <?php
            $categories = get_the_category();
            $separator = ' ';
            $output = '';
            if($categories){
                foreach($categories as $category) {
            if($category->name !== 'name-of-the-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);
            }
        ?>
        <?php the_title(); ?>
        <?php the_excerpt(); ?>
    <?php endwhile; ?>
<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php __('No News'); ?></p>
<   ?php endif; ?>
0

There are 0 best solutions below