WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Conditionally replace text within the_title for posts

[Resolved] Conditionally replace text within the_title for posts

  • I am designing a website for iPhone repair services and I want to use the CSS text-transform:uppercase for my post titles, however, I need to also maintain the lowercase”i” in iPhones. So, I thought I would be able to create a function to do this using the ‘strpos’ and ‘preg_replace’ but my code is not working and I can’t figure out what is wrong with it!

    I know the expression is evaluating because I can affect the output of the ‘else’ – it just never sees “iphone” so it always goes to ‘else’ and I am not sure why.

    <h1 class="first"><?php if (strpos("the_title()",'iPhone') !== false) {
    $string = get_the_title();
    $pattern = '/iPhone/';
    $replacement = '<span id="lil-i">i</span>Phone';
    echo preg_replace($pattern, $replacement, $string);}
    else
    echo the_title();
    ?></h1>
Viewing 1 replies (of 1 total)
  • I figured it out!

    I had to pass the_title to a variable and then use the new variable in the strpos function.

    <?php $title= get_the_title();?>
    <h1 class="first"><?php if (strpos("$title",'iPhone') !== false) {
    $string = get_the_title();
    $pattern = '/iPhone/';
    $replacement = '<span id="lil-i">i</span>Phone';
    echo preg_replace($pattern, $replacement, $string);}
    else
    echo the_title();
    ?></h1>
Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Conditionally replace text within the_title for posts’ is closed to new replies.