How to Add the Page Slug to Body Class in WordPress

Are you looking for a way to add a WordPress page slug in the body class of your theme?

Adding a page slug in the WordPress theme lets you do all the customizations you want without any complications. You can easily change the colors, fonts, and other certain items based on specific pages.

In this article, we will show you how to add a page slug in the body class of your WordPress themes.

Why Add a Page Slug in Body Class in Your Theme?

If you’re looking to customize specific pages on your site and want to correctly identify the page, then adding a page slug in the body class of your theme is really useful.

By default, your WordPress site will only show the post ID classes, which can be tricky when it comes to recognizing the correct page. A page slug shows the URL of your blog post, making it easier to customize the page.

Besides that, you can perform different customizations to your pages using a page slug body class. For example, you can change the font and colors of a particular post or highlight a call to action button on a specific landing page.

That being said, let’s look at how you can add the page slug in the body class of your WordPress theme.

Adding a Page Slug in Your WordPress Theme

To help you add the URL of your page in the body class of your WordPress theme, you can enter the following code in your theme’s functions.php file.

//Page Slug Body Class
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );

You can access your theme’s functions.php files by going to the WordPress Theme Editor (Code Editor). However, directly editing the theme files is very risky.

That is because any mistake while adding the code can break your site and block you from logging into your WordPress dashboard.

A much simpler way to add the code to the body class of your theme is by using a WordPress plugin like Code Snippets.

You can easily add code to your site within minutes and without any errors using the plugin. Plus, it also ensures that your code doesn’t get removed if you update or change your theme in the future.

First, you’ll need to install and activate the Code Snippets plugin on your site. For more details, you can refer to our guide on how to install a WordPress plugin.

Next, you can navigate to Snippets from your WordPress admin panel. After that, go ahead and click the ‘Add New’ button.

Add new custom code snippet

Now, give a title for your code snippet and enter the code under the ‘Code’ section.

Enter code snippet

Once you’ve added the code, don’t forget to click the ‘Activate’ button. For more details, you can refer to our guide on how to add custom code in WordPress.

You will now start seeing a new body class being outputted like this: page-{slug}. Use that class to override your default styles and customize elements for specific pages.

For example, let’s say you wanted to style your sidebar widgets, but only on a page that has the slug “education.” In that case, you could add CSS like this:

#sidebar .widgets{background: #fff; color: #000;}
.page-education #sidebar .widgets{background: #000; color: #fff;}

For more details, you can see our guide on how to add custom CSS to WordPress.

We hope this article helped you learn how to add a page slug in the body class of your WordPress theme. You may also want to look at our guide on WordPress body class and how to choose the best design software.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.