0

Here's a little coding problem for you. The code:

if ($categories) { 

    foreach ($categories as $tag) { 

        $tag_link = get_category_link($tag->term_id); 

        //new line 
        if (isset($instance['new-line'])) { 
            $html .= "<div>"; 
        } 

        $html .= "<span class="tagsy-box" style="$box_style">"; 
        $html .= "<a href="{$tag_link}" title="{$tag->name} Tag" class="{$tag->slug}"><span class="tagsy-name-$style $theme_name" style="$name_style">"; 
        $html .= "{$tag->name}</span><span class="tagsy-count-$style $theme_count" style="$count_style">{$tag->count}</span></a></span>"; 

        //new line 
        if (isset($instance['new-line'])) { 
            $html .= "</div>"; } } 
        } 
        else { 
            $html .= "No Categories Available"; 
        }

    }

}

The problem: I want to exclude certain categories from displaying. Apparently, the way to do this is to skip the foreach loop for those categories I want to exclude. Unfortunately, I have no idea how to do this. Help much appreciated.

3
  • Please show the code that populates $categories Commented Aug 9, 2013 at 20:11
  • Also, I'm assuming your code is spread over several lines and not mostly commented out as it is now, so please reformat it. Lines such as $html .= "<span class="tagsy-box" style="$box_style">"; will throw errors, as you are nesting unescaped double quotes. Commented Aug 9, 2013 at 20:18
  • Here's the whole file: pastebin.com/rixJp07d Relevant bit starts at line 197 (I think). Commented Aug 9, 2013 at 20:35

2 Answers 2

2

In line 195 you're using get_categories, which can be filtered to exclude specific category numbers, either through an array of arguments, or a query string. Since $options is already set, you could add to it before you call get_categories:

if ($show_assigned_categories == 'true' && is_single()) {
        $categories = get_the_category();
} 
else {
    $options['exclude'] = '1,2,3'; // your comma-separated list goes here
    $categories = get_categories($options);
}
6
  • I tried this but it broke my site. All that would load is a white screen... Commented Aug 9, 2013 at 23:37
  • Hmm. What values did you use for $options['exclude']? Commented Aug 10, 2013 at 1:42
  • I tried '1, 48' Commented Aug 10, 2013 at 12:26
  • 1
    A white screen means PHP ran into a a fatal error. You can often see the error in the browser by activating WordPress Debug. Do not leave it activated on a production site. Commented Aug 10, 2013 at 15:33
  • I don't know if it'll make a difference, but try removing the space between the comma and the 4. None of the examples I see online have one in there. Commented Aug 12, 2013 at 15:06
0

To exclude categories you can try this code.

$categories = get_categories();    
$category_to_exclude = array(1,2,3);

if ($categories) {
    foreach ($categories as $tag) {
        if( !in_array($tag->term_id, $category_to_exclude) ) {
            $tag_link = get_category_link($tag->term_id);
            echo $tag_link;
        }
    }
}

The in_array php function will check the current foreach loop $tag->term_id is exists in $category_to_exclude variable. If it not exist, the looping will continue. If it exist, statement will be false and skip the category id.

2
  • Please add an explanation to your answer: why could that solve the problem? Commented Aug 10, 2013 at 9:11
  • Explained, hope it helps. Commented Aug 10, 2013 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.