WordPress Plugin: Add URL Slugs to <body> Class

Funny, I had built this some time back for my own personal use, thought it could be useful to others, planned on pushing it to the WordPress Repository, then apparently never got around to it…

Anyhow, another quick and simple WP plug-in. This one deconstructs a page’s URL and, after filtering out the site’s domain name, adds anything separated by a “/” as an additional class to the <body> tag.

So, if your page’s URL is:
https://aarontgrogg.com/2012/02/24/wordpress-plugin-add-url-slugs-as-body-classes/
Your page’s <body> tag would have at least these classes:
"2012 02 24 wordpress-plugin-add-url-slugs-as-body-classess"

While the above example isn’t all that useful, it gets a little stronger when you get to category pages, like…
https://aarontgrogg.com/category/wordpress/
which would give you:
"category wordpress"
or Pages, like…
https://aarontgrogg.com/contact/
which would give you:
"contact"
or even better, if we use nested Pages or Custom Post Types, like…
https://aarontgrogg.com/movies/star-trek/wrath-of-khan/
which would give you:
"movies star-trek wrath-of-khan"

Now start thinking about how CSS can come into play when you suddenly have those additional classes on your <body> tags…

Downloadable now from the WordPress Repository:
http://wordpress.org/extend/plugins/add-url-slugs-as-body-classes/

Happy WordPressing,
Atg

8 Responses to WordPress Plugin: Add URL Slugs to <body> Class

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.