Add class with page id to body tag WordPress

Written by on June 24, 2011 in WordPress - No comments
wordpress-page-id

If you’re working in WordPress you will most likely come across a need to target one specific page with CSS. Since every page in WordPress has a unique ID it’s a great idea to dynamically add the page ID to the body tag as a class. This way you can target anything within the body tag specifically. Simply add the following to the body tag in the header.php of your website theme.

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

Here is an example output, you’ll notice it also adds other great classes (allowing you other ways of targeting specific pages or groups of pages based on certain characteristics).

<body class="page page-id-1076 page-template page-template-default logged-in">

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Check him out on Twitter, Facebook, Google+ and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.