The following is a guest post by Trishah Woolley. Over the years, Trishah has collected a ton of code snippets that to the job of adding IDs and classes based on some variable information, like the URL or data from WordPress. Each of these snippets were either either collected from documentation or a similar online resource, or written by Trishah based on standard features of the language and API's involved.
Over the time I have been doing WordPress web design, there are occasions when I have needed to add an ID or class to the <body>tag. For example: This often happens when integrating a 3rd party into WordPress that creates its own pages that WordPress doesn't recognize. Or I need more nuanced classes for the site design I'm creating.
To change the look of these pages in a way that the styles are scoped just to that page, we need a way to target just that page or category of pages. Below are some of the ways I have found helpful to add IDs and classes to the <body>of both non-WordPress (but still PHP) and WordPress pages.
#01 - Add ID from the URL
This will strip the forward-slash, `.php`, and the `?pg=`, from the URL and use the rest as an ID. Change the array to match your particular situation.