WordPress: If the_category != “category2”

Quick WordPress question. Is it possible to check against a specific category, so not to display it? I tried this, but my category was still being echoed (no errors).

 <?php if (the_category() != "NAMEOFMYCATEGORY") { the_category(' | '); } ?>

Or would I need to work with a new function?

Read More

To clarify: I want to HIDE 1 specific category so it doesn’t show up.

Related posts

Leave a Reply

4 comments

  1. This should work:

    <?php
    foreach (get_the_category() as $category) {
        if ( $category->name !== 'FORBIDDEN CATEGORY NAME' ) {
            echo '<a href="' . get_category_link($category->term_id) . '">' .$category->name . '</a><br />'; //Markup as you see fit
        }
    

    The name is Caps sensitive.

  2. Why not to use the codex version?

    if (is_home()) {query_posts('cat=-1,-2,-3'); }  // excludes categories 1 2 3
    

    also do you remember in_category() ?

    if (have_posts() && (!in_category('3')) {
    
    //do domething;
    
    } else // do different loop
    
  3. I think you need to do something like this, if i got ur question correctly 🙂

    foreach((get_the_category()) as $category) {
       if($category->cat_name = 'mycheckcatname')
       {
       DO THIS
       }
       else
       {
       Do THAT
       }
    }
    

    NEW EDIT

    or this is what else u are looking for —

    <?php if (is_category('Category A')) : ?>
    <p>This is the text to describe category A</p>
    <?php elseif (is_category('Category B')) : ?>
    <p>This is the text to describe category B</p>
    <?php else : ?>
    <p>This is some generic text to describe all other category pages, 
    I could be left blank</p>
    <?php endif; ?>
    
  4. <?php 
        $categories =  get_categories('');
        $excluded_categories = array('Sem Categoria','Uncategorized');
    
        foreach  ($categories as $category) {
            if(in_array( $category->cat_name, $excluded_categories)){
                continue;
            }
            echo $category->name;
        }                                     
    ?>