• Resolved Argentum

    (@argentum)


    Hi,

    I use this plugin to add categories and tags to pages.
    https://sv.www.ads-software.com/plugins/add-category-to-pages/

    I want to style the header title bar and have different background-color css rules based on the category of the page.

    body.category-slug-1 .page-header {background-color:#73a1d5 !important; color: #ff0000; }

    body.category-slug-2 .page-header {background-color:##73a1d5 !important; color: #ff0000; }

    Can I somehow edit the page template to make the category added to the body tag of the pages?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    You can try a plugin ‘Custom Body Class’ to do it. Let me know it works or not.

    Thread Starter Argentum

    (@argentum)

    I would rather have the already assigned categories somehow printed out in the body tag. I have quite a lot of pages. Your suggestion could work, though it would need a load of work. The posts have their categories printed out in the body tag. Is there no way to do the same with pages?

    Best regard
    A

    You can try the below code and edit it as per your need. Hope it helps you. Add it to the functions.php file of the child theme.

    add_filter( 'body_class', 'my_page_body_class');
    function my_page_body_class( $classes ) {
         if ( is_page() || is_category() )
              $classes[] = 'add-class';
     
         return $classes; 
    }
    Thread Starter Argentum

    (@argentum)

    Ok, I am not very fluent in PHP nor in WordPress coding.

    I add your code and every page get the ‘add-class’ in the body tag. But where do I add the wanted category? The pages are already assigned to categories.

    I tried this
    ‘if ( is_page() && is_category(6) )
    $classes[] = ‘wanted-category’;’

    ‘if ( is_page() && is_category(‘6’) )
    $classes[] = ‘wanted-category’;’

    ‘if ( is_page() && is_category(‘category-slug’) )
    $classes[] = ‘wanted-category’;’

    *whine* ??

    The code I gave you is the example. You need to edit it as per your need. You can also hire a developer for this.

    Thread Starter Argentum

    (@argentum)

    I changed to the in_category(‘6’) function, and now it works … Thanks.

    Glad to hear that it is fixed!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add categories to page template body tag’ is closed to new replies.