Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #279829

    Hello,

    Is there a way to get the category nice name of a portfolio category, and write that out as a class or id in the body of a portfolio single?

    I’m in the process of making a portfolio site with 3 pages (Film, Commercial, Art) that each have their own set of portfolio singles (categorized as Film, Commercial, and Art) and I’d like to highlight the Film navbar link whenever a site visitor is viewing a Film Portfolio Single. If I can add those category specific classes to the <html> or <body> element of each page than I can style the corresponding navbar link to be highlighted.

    #280427

    Hey juicesandmilks!

    Yes, I found some tutorials with google when I searched for “wordpress add category name to body class” – i.e. http://bavotasan.com/2011/add-a-posts-category-name-to-body-class-in-wordpress/

    Best regards,
    Peter

    #283379

    Hi Dude, and thanks for the pointer.

    Unfortunately that bit of code does not work for me. The function is adding a ‘category-‘ class to my body tag, but it either isn’t able to find the category slug, or it isn’t able to add it to the class name:

    function add_category_name($classes = '') {
       if(is_single()) {
          $category = get_the_category();
          $classes[] = 'category-'.$category[0]->slug; 
       }
       return $classes;
    }
    add_filter('body_class','add_category_name');
    #283390

    Hi!

    That’s because that code is meant to be used with Posts, try changing it to:

    function add_category_name($classes = '') {
       if(is_single()) {
          $category = get_the_terms($post->ID, 'portfolio_entries');
          $classes[] = 'category-'.current($category)->slug; 
       }
       return $classes;
    }
    add_filter('body_class','add_category_name');
    

    Regards,
    Josue

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.