• Resolved Jack

    (@moxie)


    Currently the class names only exist of a (limited) number of tags, but not the category. How can I add that? I need to modify the colors a little bit, but the category class is missing or the tag I could use is not listed. There seems to be a maximum of 3 tags.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there,

    To get the result as you desired, create a child theme, and copy a file that you want to modify and locate the body_class tag in the code and add your classes.

    Thanks,
    Ashesh

    Thread Starter Jack

    (@moxie)

    Hi Ashesh,

    The only place where I can find the body_class is in the header.php file and in some function, but I don’t see how I could add something like the_category() to those classes. I little guidance would be appreciated.

    <body <?php body_class(); ?>>

    Hi @moxie,

    Please specify where do you want to modify color.

    Thanks,
    Ashesh

    Thread Starter Jack

    (@moxie)

    I want to add a few tweaks to single post pages and to posts shown in the general category pages if they belong to a certain category, in this case the category recipes. A friend writes general blogs but also adds recipe posts, and she wants the recipes to be recognizable as recipes. So I want to change the colors here and there and perhaps a small background image in a corner. Normally I can do that, just by using the category class name that is placed in the body and article tag, but your theme is missing these classnames. ??

    Hi @moxie,

    You can create a custom category template via child theme and customize it according to your need. You should have proficient knowledge about WordPress and some coding knowledge to achieve your result.

    Reference: https://www.wpbeginner.com/wp-themes/how-to-create-category-templates-in-wordpress/

    Thanks,
    Ashesh

    Thread Starter Jack

    (@moxie)

    Hi Ashesh. Thanks for the suggestion. Custom templates was too circuitous for this, so I searched and found this code to add extra classnames to body_class.

    add_filter('body_class','add_category_to_single');
      function add_category_to_single($classes) {
        if (is_single() || is_category() || in_category() ) {
          global $post;
          foreach((get_the_category($post->ID)) as $category) {
            $classes[] = $category->category_nicename;
          }
        }
        return $classes;
      }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add category class name to Body and Article’ is closed to new replies.