如何替换wordpress中的文本the_title?

时间:2016-05-26 17:55:26

标签: php wordpress wordpress-plugin wordpress-theming

我想替换wp标题中的文字,
实际上,有一个流行帖子的小部件。我想替换这个小部件输出中的文本,所以我查看了小部件功能,我找到了这段代码:

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...

我尝试将此行<?php the_title(); ?>更改为:

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

但它在现场没有任何作用,也没有任何影响! 如何在窗口小部件输出中替换标题中的文本?
如果有两个字符串我想要替换它怎么可能? 我应该使用这样的东西吗?:

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

2 个答案:

答案 0 :(得分:2)

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

替换为

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

答案 1 :(得分:1)

试试这个

$wptitle = get_the_title( get_the_ID() );
$wptitle = str_replace('textbefore', 'textafter', $wptitle);
echo $wptitle;