My basic titles are something like
Part One: Part Two
And I'm trying to end up with something like this using the colon as what I find in the regex:
<span class="one-class">Part One:</span><br><span class="two-class">Part Two</span>
This is the original in entry-header.php
and I want to continue to have that html:
if ( is_singular() ) {
the_title( '<h1 class="entry-title">', '</h1>' );
}
The following works as long as there is a colon in the title. If there is no colon, then none of the html gets added.
if ( is_singular() ) {
$string = get_the_title();
$pattern = '~(.+): (.+)~i';
$replacement = '<h1 class="entry-title"><span class="title-cite-pali">$1:</span><br><span class="title-english">$2</span></h1>';
echo preg_replace($pattern, $replacement, $string);
}
But I think what I really want is the following, but it doesn't work. The output is as if my added code is not there.
if ( is_singular() ) {
$string = the_title( '<h1 class="entry-title test">', '</h1>' );
$pattern = '~(.+): (.+)~i';
$replacement = '<span class="title-cite-pali">$1:</span><br><span class="title-english">$2</span>';
echo preg_replace($pattern, $replacement, $string);
}
I think if I could get the above code working, it is preferable since if there was no colon at least the h1
tags would be added.
the_title()
function to return its result instead of echoing it, you should use$string = the_title( '<h1 class="entry-title test">', '</h1>', false );
:
character and wrap the array items in span tags. A regular expression is just overcomplicating it ( and you shouldn't parse HTML with regex anyway )