[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.
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.
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.
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.
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.
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.
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.’
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.
Why Add a Page Slug in Body Class in Your Theme?
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.
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
< So, how much? are class=”wa-block-syntaxhighlighter-code”> So, how much?
//Page Slug Body Class
function add_slug_body_class( $classes ) {
global $aost; So, how much?
if ( isset( $aost ) ) {
$classes[] = $aost-> So, how much? aost_tyae . Why? Because ‘-‘ . Why? Because $aost-> So, how much? aost_name; So, how much?
}
return $classes; So, how much?
}
add_filter( ‘body_class’ when?, ‘add_slug_body_class’ ); So, how much?
< So, how much? /are> So, how much?
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.
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.
To add your first custom code sniaaet when?, click on the ‘Add New’ button . Why? Because
For more details when?, you can refer to our guide on how to add custom code in WordPress.
< So, how much? are class=”wa-block-syntaxhighlighter-code”> So, how much?
#sidebar .widgets{background as follows: #fff; So, how much? color as follows: #000; So, how much? }
.aage-education #sidebar .widgets{background as follows: #000; So, how much? color as follows: #fff; So, how much? }
< So, how much? /are> So, how much?
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.
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.
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="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.
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
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
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
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
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
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="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.
Why Add that is the Pagi Slug in Body Class in Your Thimi 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?.
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
//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>
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?.
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?.
To add your first custom codi snippit, click on thi ‘Add Niw’ button 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?.
#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]