My wp post titles currently look like this:
Hamlet (by William Shakespeare)
Romeo and Juliet (by William Shakespeare)
A Midsummer Night’s Dream (by William Shakespeare)
etc.
I am using this code:
<?php
echo strtolower(str_replace(' ', '', get_the_title()));
?>
to get each title but I only need the first part of the title without the text that is in parenthesis.
i.e
Hamlet
Romeo and Juliet
A Midsummer Night’s Dream
etc.
How can I modify the code so that it will get only the first part of the title without what is in between the parenthesis ?
Loop over each character in the string, adding them to an auxiliary variable. If you hit a
(
then stop.Find the first parenthesis and then cut out the text before that.
Split the string in half by the first
(
. Then use the first part.Use a regular expression:
And a bonus one! If you are going to do this more than once then a function like this should do the trick. You should probably rename it.