Wordpress - Add class to main div in specific page
P粉449281068
P粉449281068 2024-03-28 15:23:19
0
2
295

I have a specific page and I just need to change the background color of that page, the page structure starts like this:

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

I want to put functions.php, for example:

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 any way to add it? ..thank you all! ! cheers!

P粉449281068
P粉449281068

reply all(2)
P粉914731066

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

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

..., WordPress will automatically add the "body class", which has a page-id-xxx, where "xxx" represents a separate id number for each page, corresponding to the page id in the database. You can then create a CSS rule with background-color for that id (#page-id-xxx { background-color: #abc; } and add it to Generic stylesheet or just access the page through the customizer.

P粉178132828

Finally, I decided to apply full transparency to all levels of container elements within the body:

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

I can use add_filter( 'body_class', 'my_css'); in the body whenever applicable I said in the post that this would make my life easier... Thanks anyway @Johannes, @Broe, @Rahul Srivastava! cheers! ! !

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template