WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_category exclude categories (13 posts)

  1. kangel
    Member
    Posted 4 years ago #

    I have an area in each post where it lists which categories the post is tagged in.
    Each post is tagged with at least 3 categories (necessary for site navigation). However, where I call for the category listing, I would like to hide a category from appearing.

    I'm using <?php the_category(' '); ?>

    Is there a way to add an exclude to the_category script?

    query_posts doesn't seem to work.

  2. Kristian Themstrup
    Member
    Posted 4 years ago #

    Sure is. Use the code below in your theme functions.php file. I also included conditionals here as well in case someone wants to exclude something based on that. The first $exclude sets the global settings (these categories will be excluded always - cat-slug1, cat-slug2), and the others push additional variables onto $exclude in certain scenarios. You don't need the conditional lines, but I did, so I thought I'd share them.

    // Exclude selective categories from the_category - GLOBAL SETTINGS
    function the_category_filter($thelist,$separator=' ') {
        if(!defined('WP_ADMIN')) {
            //list the category names to exclude
            $exclude = array('cat-slug1','cat-slug2');
            if(is_category('2')) {
            array_push($exclude,'cat-slug3','cat-slug4'); }
            if(is_category('3')) {
            array_push($exclude,'cat-slug5'); }
            $cats = explode($separator,$thelist);
            $newlist = array();
            foreach($cats as $cat) {
                $catname = trim(strip_tags($cat));
                if(!in_array($catname,$exclude))
                    $newlist[] = $cat;
            }
            return implode($separator,$newlist);
        } else
            return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    Much credit to Nick for help on this.

  3. keesiemeijer
    moderator
    Posted 4 years ago #

    use wp_list_categories

    <ul>
    <?php
    wp_list_categories('orderby=name&show_count=1&exclude=10,5,6'); ?>
    </ul>
  4. keesiemeijer
    moderator
    Posted 4 years ago #

    Sorry that last one won't help. But this will:
    put the following in your functions.php

    function exclude_post_categories($excl=''){
       $categories = get_the_category($post->ID);
          if(!empty($categories)){
          	$exclude=$excl;
          	$exclude = explode(",", $exclude);
          	foreach ($categories as $cat) {
          		$html = '';
          		if(!in_array($cat->cat_ID, $exclude)) {
          		$html .= '<a href="' . get_category_link($cat->cat_ID) . '" ';
          		$html .= 'title="' . $cat->cat_name . '">' . $cat->cat_name . '</a>';
          		echo $html;
          		}
    	      }
          }
    }

    You can call this function in your template inside the loop where you want your categorylinks to appear:
    <?php exclude_post_categories('1,5'); ?>

    just change the comma seperated list with your category id's. They will be excluded

  5. kangel
    Member
    Posted 4 years ago #

    Awesome.
    I tried keesie's code first, since I want the categories to appear on some pages but not all. It did the trick! Thanks for the help!

  6. km2382
    Member
    Posted 4 years ago #

    @keesiemeijer,
    based on your code above, what would be the best way to add in a separator, but not have it show up after the last link was outputted (for example, not have a "," show up after the last link)

  7. ocshawn
    Member
    Posted 3 years ago #

    this worked for me

    function exclude_post_categories($excl='', $spacer=' '){
       $categories = get_the_category($post->ID);
          if(!empty($categories)){
          	$exclude=$excl;
          	$exclude = explode(",", $exclude);
    		$thecount = count(get_the_category()) - count($exclude);
          	foreach ($categories as $cat) {
          		$html = '';
          		if(!in_array($cat->cat_ID, $exclude)) {
    				$html .= '<a href="' . get_category_link($cat->cat_ID) . '" ';
    				$html .= 'title="' . $cat->cat_name . '">' . $cat->cat_name . '</a>';
    				if($thecount>1){
    					$html .= $spacer;
    				}
    			$thecount--;
          		echo $html;
          		}
    	      }
          }
    }

    Format like
    <?php exclude_post_categories('1,5',', '); ?>

  8. ingvi
    Member
    Posted 3 years ago #

    Wow people thanks for this, it's just what I was looking for. Can anyone explane it to me how i can separate the outcome with a comma or a space? Its showing up in a straight line with no space.

    Also I have a parent category with many children and I have to write all the children to exclude. Is there a way to just declare the parent and it would not show the children.

    Thanks again.

  9. ando.steinmetz
    Member
    Posted 3 years ago #

    Hi,

    I've just modified this code to exclude all children of a parent category, a feature that I saw requested elsewhere, but those threads were closed so I figured I'd share the code here.

    The code is giving me one problem, though, if parent categories are removed, it's adding the separator as needed, but if there are no categories that need removing, it doesn't add the separator. Any ideas anybody?

    Code below:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  10. ando.steinmetz
    Member
    Posted 3 years ago #

    Aha. Just figured the problem that was causing the separators to drop out.

    Modified code below:
    http://pastebin.com/2zTFwxHK

    also, note that the syntax of the call is changed so that the Parent Categories to exclude are referenced by name rather than by id. ID would be better, but I was having trouble getting the parent categories' IDs. So the function would be called like:

    <?php exclude_post_categories('Excluded',', '); ?>

  11. Aaron Reimann
    Member
    Posted 3 years ago #

    Thanks ocshawn! That function helped me a lot. Thanks for sharing.

  12. claire.mcmanus
    Member
    Posted 3 years ago #

    Hello I have just used the code with the commas included and removing 3 categories (Events, Featured, Free) but only if there is more than 2 categories do the commas come into play.

    I have pasted the code from above exactly the same. Any ideas?

    You can see it working here

    http://www.thewhatwherewhen.org

    in the small white boxes with a square image to the left
    each one has:
    What: (followed by categories) – This is the <?php the_category(', ') ?>

    following the next 'What:' is this code.

    Any help would be greatly appreciated

    Thanks
    Claire

  13. claire.mcmanus
    Member
    Posted 3 years ago #

    this is interesting, it works for the one on the right but not the one on the left

    http://www.thewhatwherewhen.org/categories.png

Topic Closed

This topic has been closed to new replies.

About this Topic