• Resolved frmars

    (@frmars)


    Hello again

    I need some “administrative” categories, and do not want to show the corresponding category tags on my pages.

    Is there anaything like :

    [ the_category(exclude 10,15) ]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Where are you listing the categories?

    If you are doing it in the loop, you can find more info here:http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category

    If you are listing the categories, you can find more info here: http://codex.wordpress.org/Template_Tags/wp_list_categories#Include_or_Exclude_Categories

    If you are wanting to display those posts, even though they have those categories, then you will need to use get_the_category:

    For example this will exclude the category ‘cat_to_exclude’ from displaying

    <?php
    foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'cat_to_exclude') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>

    Thread Starter frmars

    (@frmars)

    I adapted all these indications to my needs and it did the job nicely.

    Thank you.

    Could someone please tell me how I could use the code above to exclude more than one category from the list? I can use the code to exclude one, but need to exclude several categories.

    Hello Mav300, I do not know if this is very ortodox, but it works for me:

    <?php
    foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'cat_to_exclude') {
        if ($category->cat_name != 'cat_to_exclude2') { echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';}
    }
    }
    ?>

    I don’t know how to explain it… but it works on the last version of wordpress to exclude cat_to_exclude and cat_to_exclude2

    After the first if a { start what will happen for this if… and in this happening there is another if and the happening for both of them, what is the echo, what will be print on the final document…
    the structure is something like this:
    foreach{
    if{ if{happening}}
    }
    …i told you, so hard to explain it because i just understand the syntax…. somebody explaining this please?

    good luck!!

    hello again! I’ve been talking with a friend who knows about PHP he’s written another way to do it:

    <?php
    foreach((get_the_category()) as $category) {
    if ($category->cat_ID != 'cat_to_exclude' && $category->cat_ID != 'cat_to_exclude2' && $category->cat_ID != 'cat_to_exclude3') {
    
    echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Cortos de %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>&nbsp; &nbsp;  ';
    
    }
    }
    ?>

    Take care!!!!

    I adapted that so it’s easier to list the category IDs you want to exclude
    I also wanted the list to be comma separated

    $excludedcats = array(328,338,339);
    					$count = 0;
    					$categories = get_the_category();
    					foreach($categories as $category) {
    						$count++;
    						if ( !in_array($category->cat_ID, $excludedcats) ) {
    							echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Cortos de %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
    
    							if( $count != count($categories) ){
    								echo ", ";
    							}
    
    						}
    					}

    If you need it in multiple places, it may be easier to put it in your theme’s functions.php file:

    function the_excluded_category($excludedcats = array()){
    	$count = 0;
    	$categories = get_the_category();
    	foreach($categories as $category) {
    		$count++;
    		if ( !in_array($category->cat_ID, $excludedcats) ) {
    			echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "Cortos de %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
    
    			if( $count != count($categories) ){
    				echo ", ";
    			}
    
    		}
    	}
    }

    And then call it in your template file like this:

    <?php the_excluded_category(array(1,328,338,339)); ?>

    Hey joesmalley, cool code.

    There is a little adjust:
    Change
    if( $count != count($categories) ){

    To
    if( $count != count($categories)-1 ){

    So it doesn’t display the last comma.

    Also the title says “Cortos de “. I deleted that spanish part.

    Greetings

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude categories from the_category() ?’ is closed to new replies.