9951 explained code solutions for 126 technologies


php-wordpressHow to use body_class() in WordPress with PHP?


The body_class() function in WordPress is used to add custom classes to the <body> element of a WordPress page. This can be used to target specific pages or elements with custom CSS.

Example code

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

Output example

<body class="home page-template page-template-page-templates page-template-full-width page-template-page-templatesfull-width-php page page-id-2 logged-in admin-bar no-customize-support">

Code explanation

  • body_class(): This is the function that adds the classes to the <body> element.
  • <body>: This is the HTML element that the classes are added to.
  • home: This is an example of a class that is added to the <body> element.

Helpful links

Edit this code on GitHub