Support » Developing with WordPress » Display custom post type categories as CSS class

  • Resolved Guido

    (@guido07111975)


    Hi,

    I want to display the categories of my custom post type as CSS class for the relevant post.

    So this is my function:

    
    function cats() {
    	$terms = (get_the_ID(), 'event_cat'); 
    		foreach ( $terms as $term) {
    			return $term->name;
    	}
    }
    

    And I include this in my list like this:

    
    $output .= '<div class="'.cats().'">'; 
    

    But now max one category is displayed per post. I guess because I return the function, instead of echoing it. But is there a way to return all categories that are attached to this post?

    Guido

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi Guido,
    Replace your function with the below code.

    function cats($echo = false) {
        $terms = get_the_terms( get_the_ID(), 'event_cat' ); 
        $post_terms = array();
        foreach ( $terms as $term ) {
    	$post_terms[] = $term->slug;
        }
        if( $echo ){
            echo implode( " ", $post_terms );
        } else{
            return $post_terms;   
        }
    }
    

    If parameter $echo is false, the function will return the categories in array format, can be used for further processing.

    If set to true, it will print the categories separated by space.

    Hope this helps.
    Good day!

    • This reply was modified 1 year, 8 months ago by  Achyuth Ajoy.
    Guido

    (@guido07111975)

    Hi Achyuth,

    Thanks!

    First, I made a mistake in my function, first line should be:

    
    $terms = get_the_terms( get_the_ID(), 'event_cat' );
    

    You solution does work, but with debug on there’s an notification:
    Warning: Invalid argument supplied for foreach() in..

    Only appears when post has no category.. so guess I need to add a check for this.

    Guido

    Guido

    (@guido07111975)

    Found a check, using wp_error.

    This will work for me:

    
    function cats() {
    	$terms = get_the_terms( get_the_ID(), 'event_cat' ); 
    	if ( $terms && ! is_wp_error( $terms ) ) : 
    		foreach ( $terms as $term ) {
    			$post_terms[] = $term->name;
    		}
    		return implode( " ", $post_terms );
    	endif;
    }
    

    Thanks again!

    Guido

    function cats($echo = false) {
        $terms = get_the_terms( get_the_ID(), 'event_cat' ); 
        if( !empty( $terms ) ) {
            $post_terms = array();
            foreach ( $terms as $term ) {
    	    $post_terms[] = $term->slug;
            }
            if( $echo ){
                echo implode( " ", $post_terms );
            } else{
                return $post_terms;   
            }
        }
    }

    Code updated to check whether the terms variable is empty, before looping.

    • This reply was modified 1 year, 8 months ago by  Achyuth Ajoy.

    You’re welcome. Try to use term slug instead of term name. Term name may contain spaces which may break your css

    • This reply was modified 1 year, 8 months ago by  Achyuth Ajoy.
    Guido

    (@guido07111975)

    Ah, of course. Did not thought about this! Thanks.

    Guido

    You’re welcome

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display custom post type categories as CSS class’ is closed to new replies.