The mother of all WordPress body tags

It took us a long time to perfect this gorgeous body tag, it includes everything you’d want as a front end developer, it inserts the name of the page you’re on into the class attribute, it adds the pages parent as the id, works on home, 404, search, tag, and category pages as well.

NOTE: we’ve updated the code to also show what template you’re using, so if you have an old version, grab it again below

NOTE: In order for this to work you’ll need to set your permalink structure to something that uses post names in the urls instead of numbers. You set that in Settings > Permalinks. We suggest /%postname%/

So without further ado, here’s it is:

Also here’s a more compact version (less readable but still works the same) if you want to save space in your template and don’t think you’ll be editing the code. You should probably just use this one:

Topics covered:
Enable Javascript