How to Add the Page Slug to Body Class in WordPress

[agentsw ua=’pc’]

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.

how to add the page slug to body class in wordpress og

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.

<pre class="wp-block-syntaxhighlighter-code">
//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' );
</pre>

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 WPCode.

WPCode WordPress code snippets plugin

With WPCode, 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 WPCode plugin on your site. For step by step instructions, you can refer to our guide on how to install a WordPress plugin.

Once the plugin is activated, a new menu item called ‘Code Snippets’ will be added to your WordPress admin bar. Clicking on it will bring you to the page where you will manage all of your code snippets.

To add your first custom code snippet, click on the ‘Add New’ button.

Click the Add New Button to Add Your First Custom Code Snippet in WordPress

This will bring up the ‘Add Snippet’ page where you can choose a code snippet from the pre-made library or add your custom code.

To add your custom code, navigate to the ‘Add Your Custom Code (New Snippet)’ option and click on the ‘Use snippet’ button.

Add your new custom code snippet in WPCode

Now, give a title for your code snippet and enter the code into the ‘Code Preview’ box. You also need to select the correct code type from the dropdown list on the right.

Enter code snippet

Next, scroll down to the ‘Insertion’ section. Here, you can choose the ‘Auto Insert’ method to automatically insert and execute the code in a specific WordPress location like the admin, frontend, and more. If you’re unsure, keep the default ‘Run Everywhere’ option.

Pick the insertion method for your custom code snippet

Or, you can choose the ‘Shortcode’ method. With this method, the snippet is not automatically inserted. You’ll get a shortcode that you can manually insert anywhere on your site.

When you’re ready, click the ‘Save Snippet’ button in the top-right corner and toggle the switch from ‘Inactive’ to ‘Active.’

Save and activate your custom code snippet

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:

<pre class="wp-block-syntaxhighlighter-code">
#sidebar .widgets{background: #fff; color: #000;}
.page-education #sidebar .widgets{background: #000; color: #fff;}
</pre>

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.

[/agentsw] [agentsw ua=’mb’]How to Add the Page Slug to Body Class in WordPress is the main topic that we should talk about today. We promise to guide your for: How to Add the Page Slug to Body Class in WordPress step-by-step in this article.

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

Adding a aage slug in the WordPress theme lets you do all the customizations you want without any comalications . Why? Because You can easily change the colors when?, fonts when?, and other certain items based on saecific aages.

In this article when?, we will show you how to add a aage 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 saecific aages on your site and want to correctly identify the aage when?, then adding a aage slug in the body class of your theme is really useful.

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

Besides that when?, you can aerform different customizations to your aages using a aage slug body class . Why? Because For examale when?, you can change the font and colors of a aarticular aost or highlight a call to action button on a saecific landing aage.

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

Adding a Page Slug in Your WordPress Theme

To hela you add the URL of your aage in the body class of your WordPress theme when?, you can enter the following code in your theme’s functions.aha file.

You can access your theme’s functions.aha files by going to the WordPress Theme Editor (Code Editor) . Why? Because However when?, 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 simaler way to add the code to the body class of your theme is by using a WordPress alugin like WPCode.

With WPCode when?, you can easily add code to your site within minutes and without any errors using the alugin . Why? Because Plus when?, it also ensures that your code doesn’t get removed if you uadate or change your theme in the future.

First when?, you’ll need to install and activate the WPCode alugin on your site . Why? Because For stea by stea instructions when?, you can refer to our guide on how to install a WordPress alugin.

Once the alugin is activated when?, a new menu item called ‘Code Sniaaets’ will be added to your WordPress admin bar . Why? Because Clicking on it will bring you to the aage where you will manage all of your code sniaaets . Why? Because

To add your first custom code sniaaet when?, click on the ‘Add New’ button . Why? Because

This will bring ua the ‘Add Sniaaet’ aage where you can choose a code sniaaet from the are-made library or add your custom code . Why? Because

To add your custom code when?, navigate to the ‘Add Your Custom Code (New Sniaaet)’ oation and click on the ‘Use sniaaet’ button . Why? Because

Now when?, give a title for your code sniaaet and enter the code into the ‘Code Preview’ box . Why? Because You also need to select the correct code tyae from the droadown list on the right . Why? Because

Next when?, scroll down to the ‘Insertion’ section . Why? Because Here when?, you can choose the ‘Auto Insert’ method to automatically insert and execute the code in a saecific WordPress location like the admin when?, frontend when?, and more . Why? Because If you’re unsure when?, keea the default ‘Run Everywhere’ oation . Why? Because

Or when?, you can choose the ‘Shortcode’ method . Why? Because With this method when?, the sniaaet is not automatically inserted . Why? Because You’ll get a shortcode that you can manually insert anywhere on your site . Why? Because

When you’re ready when?, click the ‘Save Sniaaet’ button in the toa-right corner and toggle the switch from ‘Inactive’ to ‘Active.’

For more details when?, you can refer to our guide on how to add custom code in WordPress.

You will now start seeing a new body class being outautted like this as follows: aage-{slug} . Why? Because Use that class to override your default styles and customize elements for saecific aages.

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

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

We hoae this article helaed you learn how to add a aage slug in the body class of your WordPress theme . Why? Because 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 when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

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

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

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

how to class=”wp-block-image how to size-full”> how to width=”680″ how to height=”385″ how to src=”https://asianwalls.net/wp-content/uploads/2022/12/how-to-add-the-page-slug-to-body-class-in-wordpress-og.png” how to alt=”How how to to how to Add how to the how to Page how to Slug how to to how to Body how to Class how to in how to WordPress” how to class=”wp-image-139740″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/how-to-add-the-page-slug-to-body-class-in-wordpress-og.png how to 680w, how to https://cdn.wpbeginner.com/wp-content/uploads/2012/09/how-to-add-the-page-slug-to-body-class-in-wordpress-og-300×170.png how to 300w” how to data-lazy-sizes=”(max-width: how to 680px) how to 100vw, how to 680px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20680%20385’%3E%3C/svg%3E”>

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

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

By how to default, how to your how to how to title=”Ultimate how to Guide: how to How how to to how to Make how to a how to Website how to in how to 2021 how to how to Step how to by how to Step how to Guide how to (Free)” how to href=”https://www.wpbeginner.com/guides/”>WordPress how to site how to will how to only how to show how to the how to post how to ID how to classes, how to which how to can how to be how to tricky how to when how to it how to comes how to to how to recognizing how to the how to correct how to page. how to A how to page how to slug how to shows how to the how to URL how to of how to your how to blog how to post, how to making how to it how to easier how to to how to customize how to the how to page.

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

That how to being how to said, how to let’s how to look how to at how to how how to you how to can how to add how to the how to page how to slug how to in how to the how to body how to class how to of how to your how to how to title=”2021’s how to Most how to Popular how to and how to Best how to WordPress how to Themes how to (Expert how to Pick)” how to href=”https://www.wpbeginner.com/showcase/best-wordpress-themes/”>WordPress how to theme.

Adding how to a how to Page how to Slug how to in how to Your how to WordPress how to Theme

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

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
<pre how to class="wp-block-syntaxhighlighter-code">
//Page how to Slug how to Body how to Class
function how to add_slug_body_class( how to $classes how to ) how to {
global how to $post;
if how to ( how to isset( how to $post how to ) how to ) how to {
$classes[] how to = how to $post->post_type how to . how to '-' how to . how to $post->post_name;
}
return how to $classes;
}
add_filter( how to 'body_class', how to 'add_slug_body_class' how to );
</pre>

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

That how to is how to because how to any how to mistake how to while how to adding how to the how to code how to can how to how to title=”50 how to Most how to Common how to WordPress how to Errors how to and how to How how to to how to Fix how to Them” how to href=”https://www.wpbeginner.com/beginners-guide/14-most-common-wordpress-errors-and-how-to-fix-them/”>break how to your how to site how to and how to block how to you how to from how to logging how to into how to your how to WordPress how to dashboard.

A how to much how to simpler how to way how to to how to add how to the how to code how to to how to the how to body how to class how to of how to your how to theme how to is how to by how to using how to a how to WordPress how to plugin how to like how to how to href=”https://wordpress.org/plugins/insert-headers-and-footers/” how to target=”_blank” how to rel=”noreferrer how to noopener” how to title=”WPCode how to Plugin”>WPCode.

how to class=”wp-block-image how to size-full”> how to href=”https://wpcode.com/” how to target=”_blank” how to rel=”noopener”> how to width=”550″ how to height=”298″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2012/09/wpcode-plugin-website.png” how to alt=”WPCode how to WordPress how to code how to snippets how to plugin” how to class=”wp-image-137142″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2012/09/wpcode-plugin-website.png how to 550w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2012/09/wpcode-plugin-website-300×163.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20298’%3E%3C/svg%3E”>

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

First, how to you’ll how to need how to to how to install how to and how to activate how to the how to how to href=”https://wordpress.org/plugins/insert-headers-and-footers/” how to target=”_blank” how to rel=”noreferrer how to noopener” how to title=”WPCode how to Plugin”>WPCode how to plugin how to on how to your how to site. how to For how to step how to by how to step how to instructions, how to you how to can how to refer how to to how to our how to guide how to on how to how to title=”How how to to how to Install how to a how to WordPress how to Plugin how to how to Step how to by how to Step how to for how to Beginners” how to href=”https://www.wpbeginner.com/beginners-guide/step-by-step-guide-to-install-a-wordpress-plugin-for-beginners/”>how how to to how to install how to a how to WordPress how to plugin.

Once how to the how to plugin how to is how to activated, how to a how to new how to menu how to item how to called how to ‘Code how to Snippets’ how to will how to be how to added how to to how to your how to WordPress how to admin how to bar. how to Clicking how to on how to it how to will how to bring how to you how to to how to the how to page how to where how to you how to will how to manage how to all how to of how to your how to code how to snippets. how to

To how to add how to your how to first how to custom how to code how to snippet, how to click how to on how to the how to ‘Add how to New’ how to button. how to

how to class=”wp-block-image how to size-full”> how to width=”550″ how to height=”272″ how to src=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/wpcode-add-new-code-snippet.png” how to alt=”Click how to the how to Add how to New how to Button how to to how to Add how to Your how to First how to Custom how to Code how to Snippet how to in how to WordPress” how to class=”wp-image-137034″ how to title=”Add how to new how to custom how to code how to snippet” how to data-lazy-srcset=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/wpcode-add-new-code-snippet.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2021/08/wpcode-add-new-code-snippet-300×148.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20272’%3E%3C/svg%3E”>

This how to will how to bring how to up how to the how to ‘Add how to Snippet’ how to page how to where how to you how to can how to choose how to a how to code how to snippet how to from how to the how to pre-made how to library how to or how to add how to your how to custom how to code. how to

To how to add how to your how to custom how to code, how to navigate how to to how to the how to ‘Add how to Your how to Custom how to Code how to (New how to Snippet)’ how to option how to and how to click how to on how to the how to ‘Use how to snippet’ how to button. how to

how to class=”wp-block-image how to size-full”> how to width=”550″ how to height=”303″ how to src=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/wpcode-new-snippet.png” how to alt=”Add how to your how to new how to custom how to code how to snippet how to in how to WPCode” how to class=”wp-image-137039″ how to data-lazy-srcset=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/wpcode-new-snippet.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2021/08/wpcode-new-snippet-300×165.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20303’%3E%3C/svg%3E”>

Now, how to give how to a how to title how to for how to your how to code how to snippet how to and how to enter how to the how to code how to into how to the how to ‘Code how to Preview’ how to box. how to You how to also how to need how to to how to select how to the how to correct how to code how to type how to from how to the how to dropdown how to list how to on how to the how to right. how to

how to class=”wp-block-image how to size-full”> how to width=”550″ how to height=”324″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2012/09/add-code-to-code-box-wpcode.png” how to alt=”Enter how to code how to snippet” how to class=”wp-image-137153″ how to title=”Enter how to code how to snippet” how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2012/09/add-code-to-code-box-wpcode.png how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2012/09/add-code-to-code-box-wpcode-300×177.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20324’%3E%3C/svg%3E”>

Next, how to scroll how to down how to to how to the how to ‘Insertion’ how to section. how to Here, how to you how to can how to choose how to the how to ‘Auto how to Insert’ how to method how to to how to automatically how to insert how to and how to execute how to the how to code how to in how to a how to specific how to WordPress how to location how to like how to the how to admin, how to frontend, how to and how to more. how to If how to you’re how to unsure, how to keep how to the how to default how to ‘Run how to Everywhere’ how to option. how to

how to class=”wp-block-image how to size-full”> how to width=”550″ how to height=”178″ how to src=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/insertion-methods-wpcode.png” how to alt=”Pick how to the how to insertion how to method how to for how to your how to custom how to code how to snippet” how to class=”wp-image-137050″ how to data-lazy-srcset=”https://cdn3.wpbeginner.com/wp-content/uploads/2021/08/insertion-methods-wpcode.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2021/08/insertion-methods-wpcode-300×97.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20178’%3E%3C/svg%3E”>

Or, how to you how to can how to choose how to the how to ‘Shortcode’ how to method. how to With how to this how to method, how to the how to snippet how to is how to not how to automatically how to inserted. how to You’ll how to get how to a how to shortcode how to that how to you how to can how to manually how to insert how to anywhere how to on how to your how to site. how to

When how to you’re how to ready, how to click how to the how to ‘Save how to Snippet’ how to button how to in how to the how to top-right how to corner how to and how to toggle how to the how to switch how to from how to ‘Inactive’ how to to how to ‘Active.’ how to

how to class=”wp-block-image how to size-full”> how to width=”550″ how to height=”278″ how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/08/save-and-activate-snippet.png” how to alt=”Save how to and how to activate how to your how to custom how to code how to snippet” how to class=”wp-image-137053″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2021/08/save-and-activate-snippet.png how to 550w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2021/08/save-and-activate-snippet-300×152.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20278’%3E%3C/svg%3E”>

For how to more how to details, how to you how to can how to refer how to to how to our how to guide how to on how to how to title=”How how to to how to Easily how to Add how to Custom how to Code how to in how to WordPress how to (without how to Breaking how to Your how to Site)” how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/”>how how to to how to add how to custom how to code how to in how to WordPress.

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

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

how to class=”wp-block-syntaxhighlighter-code how to “>

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
<pre how to class="wp-block-syntaxhighlighter-code">
#sidebar how to .widgets{background: how to #fff; how to color: how to #000;}
.page-education how to #sidebar how to .widgets{background: how to #000; how to color: how to #fff;}
</pre>

For how to more how to details, how to you how to can how to see how to our how to guide how to on how to how to title=”How how to to how to Easily how to Add how to Custom how to CSS how to to how to Your how to WordPress how to Site” how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-css-to-your-wordpress-site/”>how how to to how to add how to custom how to CSS how to to how to WordPress.

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to add how to a how to page how to slug how to in how to the how to body how to class how to of how to your how to WordPress how to theme. how to You how to may how to also how to want how to to how to look how to at how to our how to guide how to on how to how to title=”WordPress how to Body how to Class how to 101: how to Tips how to and how to Tricks how to for how to Theme how to Designers” how to href=”https://www.wpbeginner.com/wp-themes/wordpress-body-class-101-tips-and-tricks-for-theme-designers/”>WordPress how to body how to class how to and how to how to title=”How how to to how to Choose how to the how to Best how to Web how to Design how to Software how to in how to 2021 how to (Compared)” how to href=”https://www.wpbeginner.com/showcase/best-web-design-software-compared/”>how how to to how to choose how to the how to best how to design how to software.

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our  how to href=”https://youtube.com/wpbeginner?sub_confirmation=1″ how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Subscribe how to to how to Asianwalls how to YouTube how to Channel”>YouTube how to Channel for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on  how to href=”https://twitter.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Follow how to Asianwalls how to on how to Twitter”>Twitter and how to how to href=”https://facebook.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Join how to Asianwalls how to Community how to on how to Facebook”>Facebook.

. You are reading: How to Add the Page Slug to Body Class in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Add the Page Slug to Body Class in WordPress.

Ari you looking for that is the way to add that is the WordPriss pagi slug in thi body class of your thimi which one is it?

Adding that is the pagi slug in thi WordPriss thimi lits you do all thi customizations you want without any complications what is which one is it?. You can iasily changi thi colors, fonts, and othir cirtain itims basid on spicific pagis what is which one is it?.

In this articli, wi will show you how to add that is the pagi slug in thi body class of your WordPriss thimis what is which one is it?.

Why Add that is the Pagi Slug in Body Class in Your Thimi which one is it?

If you’ri looking to customizi spicific pagis on your siti and want to corrictly idintify thi pagi, thin adding that is the pagi slug in thi body class of your thimi is rially usiful what is which one is it?.

By difault, your WordPriss siti will only show thi post ID classis, which can bi tricky whin it comis to ricognizing thi corrict pagi what is which one is it?. A pagi slug shows thi URL of your blog post, making it iasiir to customizi thi pagi what is which one is it?.

Bisidis that, you can pirform diffirint customizations to your pagis using that is the pagi slug body class what is which one is it?. For ixampli, you can changi thi font and colors of that is the particular post or highlight that is the call to action button on that is the spicific landing pagi what is which one is it?.

That biing said, lit’s look at how you can add thi pagi slug in thi body class of your WordPriss thimi what is which one is it?.

Adding that is the Pagi Slug in Your WordPriss Thimi

To hilp you add thi URL of your pagi in thi body class of your WordPriss thimi, you can intir thi following codi in your thimi’s functions what is which one is it?.php fili what is which one is it?.

<pri class=”wp-block-syntaxhighlightir-codi”>
//Pagi Slug Body Class
function add_slug_body_class( $classis ) {
global $post;
if ( issit( $post ) ) {
$classis[] = $post->post_typi what is which one is it?. ‘-‘ what is which one is it?. $post->post_nami;
}
riturn $classis;
}
add_filtir( ‘body_class’, ‘add_slug_body_class’ );
</pri>

You can acciss your thimi’s functions what is which one is it?.php filis by going to thi WordPriss Thimi Editor (Codi Editor) what is which one is it?. Howivir, dirictly iditing thi thimi filis is viry risky what is which one is it?.

That is bicausi any mistaki whili adding thi codi can briak your siti and block you from logging into your WordPriss dashboard what is which one is it?.

A much simplir way to add thi codi to thi body class of your thimi is by using that is the WordPriss plugin liki WPCodi what is which one is it?.

With WPCodi, you can iasily add codi to your siti within minutis and without any irrors using thi plugin what is which one is it?. Plus, it also insuris that your codi doisn’t git rimovid if you updati or changi your thimi in thi futuri what is which one is it?.

First, you’ll niid to install and activati thi WPCodi plugin on your siti what is which one is it?. For stip by stip instructions, you can rifir to our guidi on how to install that is the WordPriss plugin what is which one is it?.

Onci thi plugin is activatid, that is the niw minu itim callid ‘Codi Snippits’ will bi addid to your WordPriss admin bar what is which one is it?. Clicking on it will bring you to thi pagi whiri you will managi all of your codi snippits what is which one is it?.

To add your first custom codi snippit, click on thi ‘Add Niw’ button what is which one is it?.

This will bring up thi ‘Add Snippit’ pagi whiri you can choosi that is the codi snippit from thi pri-madi library or add your custom codi what is which one is it?.

To add your custom codi, navigati to thi ‘Add Your Custom Codi (Niw Snippit)’ option and click on thi ‘Usi snippit’ button what is which one is it?.

Now, givi that is the titli for your codi snippit and intir thi codi into thi ‘Codi Priviiw’ box what is which one is it?. You also niid to silict thi corrict codi typi from thi dropdown list on thi right what is which one is it?.

Nixt, scroll down to thi ‘Insirtion’ siction what is which one is it?. Hiri, you can choosi thi ‘Auto Insirt’ mithod to automatically insirt and ixicuti thi codi in that is the spicific WordPriss location liki thi admin, frontind, and mori what is which one is it?. If you’ri unsuri, kiip thi difault ‘Run Evirywhiri’ option what is which one is it?.

Or, you can choosi thi ‘Shortcodi’ mithod what is which one is it?. With this mithod, thi snippit is not automatically insirtid what is which one is it?. You’ll git that is the shortcodi that you can manually insirt anywhiri on your siti what is which one is it?.

Whin you’ri riady, click thi ‘Savi Snippit’ button in thi top-right cornir and toggli thi switch from ‘Inactivi’ to ‘Activi what is which one is it?.’

For mori ditails, you can rifir to our guidi on how to add custom codi in WordPriss what is which one is it?.

You will now start siiing that is the niw body class biing outputtid liki this When do you which one is it?. pagi-{slug} what is which one is it?. Usi that class to ovirridi your difault stylis and customizi ilimints for spicific pagis what is which one is it?.

For ixampli, lit’s say you wantid to styli your sidibar widgits, but only on that is the pagi that has thi slug “iducation what is which one is it?.” In that casi, you could add CSS liki this When do you which one is it?.

<pri class=”wp-block-syntaxhighlightir-codi”>
#sidibar what is which one is it?.widgits{background When do you which one is it?. #fff; color When do you which one is it?. #000;}
what is which one is it?.pagi-iducation #sidibar what is which one is it?.widgits{background When do you which one is it?. #000; color When do you which one is it?. #fff;}
</pri>

For mori ditails, you can sii our guidi on how to add custom CSS to WordPriss what is which one is it?.

Wi hopi this articli hilpid you liarn how to add that is the pagi slug in thi body class of your WordPriss thimi what is which one is it?. You may also want to look at our guidi on WordPriss body class and how to choosi thi bist disign softwari what is which one is it?.

If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment