skip to Main Content

I have a certain page where I need just to change background color only in that page and this page structure starts like:

<body class="...">
   <div class="start-class">
      ......
   </div>
</body>

and I was thinking to put in functions.php like:

add_filter( 'body_class', 'my_css');
function my_css( $classes ) {
     if ( is_page(82) )
        find div that has specific "start-class" because it doesn't have any id
          $classes[] = 'my-custom';
 
     return $classes; 
}

is there a way to add it?.. thanks to all!! Cheers!

3

Answers


  1. Chosen as BEST ANSWER

    At the end I decided to apply total transparency on all levels of container elements inside body with:

    background-color:rgba(0,0,0,0.0) !important;
    

    wherever applicable so I could use just a background inside body with that add_filter( 'body_class', 'my_css'); I said in the post so that makes my life easier... thanks to all anyway @Johannes , @Broe , @Rahul Srivastava ! Cheers !!!


  2. If you change / add the following php code to your body tag in the header.php file…

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

    …, WordPress will automatically add "body classes", among them a page-id-xxx, where "xxx" stands for an individual id number per page that corresponds to the page id in the database. Then you can create a CSS rule with a background-color for that id (#page-id-xxx { background-color: #abc; } and add it either to the general stylesheet or via the customizer only to that page.

    Login or Signup to reply.
  3. Use below code to add body class for specific pages.

    add_filter('body_class', 'custom_body_class');
    
        function custom_body_class($classes) {
            if (is_page(38034))
                $classes[] = 'new-class';
            return $classes;
    }
    

    Based on custom class you can change the background color of specific pages.also visit https://developer.wordpress.org/reference/functions/body_class/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search