How to replace text in wordpress the_title?

I want to replace a text in wp title ,
Actually, There’s a widget for popular posts.
I want to replace a text in output of this widget, So I looked around the widget functions and I found this code :

foreach($popular as $post) :
        setup_postdata($post);
        ?>
    <li>
        <a href="<?php the_permalink(); ?>">
            <?php if ( $show_thumb3 == 1 ) : ?>
                <?php the_post_thumbnail('widgetthumb',array('title' => '')); ?>
            <?php endif; ?>
            <?php the_title(); ?>   
        </a>
blah blah blah...

I’ve tried to change this line <?php the_title(); ?> to :

Read More
<?php
    $wptitle = the_title();
    $wptitle = str_replace('textbefore', 'textafter', $wptitle);
    echo $wptitle;
?>

But it did nothing in the live site and nothing affected at all!
How can I replace a text in the title in widget output?
How will it be possible if there are two strings that I want to replace ?
Should I use something like this ?:

    <?php
    $wptitle = the_title();
    $wptitle = str_replace('textbefore', 'textafter', $wptitle) && str_replace('text2before', 'text2after', $wptitle);
    echo $wptitle;
?>

Related posts

Leave a Reply

2 comments

  1. <?php
    $wptitle = the_title();
    $wptitle = str_replace('textbefore', 'textafter', $wptitle);
    echo $wptitle;
    ?>
    

    Replace with

    <?php
    $wptitle = get_the_title($post->id);
    $wptitle = str_replace('textbefore', 'textafter', $wptitle);
    echo $wptitle;
    ?>