Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

<?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;
?>
like image 216
M. Safari Avatar asked Oct 23 '25 03:10

M. Safari


1 Answers

<?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;
?>
like image 102
Deepti chipdey Avatar answered Oct 25 '25 17:10

Deepti chipdey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!