WordPress title as id
- Started
- Last post
- 6 Responses
- thatboyneave
Does anyone know how to get Wordpress to output the title or slug of a page in a format that can be used inside an id or class in HTML?
So for example, if the page title is
‘Sweet & Sour Pork—only $5'You could have <body class="sweet-sour-pork-only-5">
Thanks in advance.
- plash0
yes but it would have to be *exact*
- ukit0
Something like this maybe?
- along with http://php.net/manua…ukit
- that hurts my brain.plash
- Practical example: http://www.kirupa.co…
(top line of code)ukit
- thatboyneave0
Brilliant thanks.
- thatboyneave0
This actually worked a treat with only one line - from the Kirupa link ukit posted.
strtolower(preg_replace('/[^a-zA... , '' , get_the_title()));
It strips out all the spaces rather than replacing them with hyphens but that works just as well for my purposes.
Thanks again.
- ukit0
Well you can just insert a hyphen in those single quotes and it should add the hyphen (I think). preg_replace is regular expressions BTW. Which is something that truly makes my brain hurt, but is a powerful tool for selecting and transforming text.
- acescence0
much simpler:
echo basename( get_permalink() );- if you want the slugacescence
- Sweet. Quick way to strip the domain and directories out so you just get the useful bit at the end?thatboyneave
- yesacescence