Okay
  Public Ticket #455268
Add category class to body for single portfolio items
Closed

Comments

  • Marc started the conversation

    Hi guys,

    I need to add the Portfolio category as a body class on single portfolio pages.

    I usually use the following code to achieve this (from the WP codex), but it doesn\'t seem to work on Salient single portfolio pages:

    function category_id_class( $classes ) {
    global $post;
        foreach ( get_the_category( $post->ID ) as $category ) {
    $classes[] = $category->category_nicename;
        }
        return $classes;
    }
    add_filter( \'post_class\', \'category_id_class\' );
    add_filter( \'body_class\', \'category_id_class\' );
    

    It does however work on single post pages!

    Please advise as to how to get this working for portfolio single pages.

  • Marc replied

    OK, just figured this one out myself...

    I realised that Portfolio Categories aren\'t actually categories, but a custom taxonomy term with slug \'project-type\', hence the reason why the code wasn\'t working. To get the portfolio category added as a body class to single portfolio items, use the following code in your child themes functions file:

                // Add custom taxonomy terms to body class
    function custom_taxonomy_in_body_class( $classes ){
    if( is_singular() )
    {
    global $post;
    $custom_terms = get_the_terms($post->ID, \'project-type\');
    if ($custom_terms) {
    foreach ($custom_terms as $custom_term) {
    $classes[] = $custom_term->slug;
    }
    }
    }
    return $classes;
    }
    add_filter( \'body_class\', \'custom_taxonomy_in_body_class\' );