【问题标题】:How to replace text in wordpress the_title?如何替换wordpress the_title中的文本?
【发布时间】:2016-05-26 17:55:26
【问题描述】:

我想替换 wp title 中的文本,
实际上,有一个热门帖子的小部件。
我想在这个小部件的输出中替换一个文本,所以我查看了小部件的功能,发现了这段代码:

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

我尝试将这一行 &lt;?php the_title(); ?&gt; 更改为:

<?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;
?>

【问题讨论】:

  • the_title() 函数用于显示帖子标题,并且该热门帖子插件还显示帖子部分的热门帖子,因此如果您想更改某些内容或想要添加内容,您可以附加到 the_title() 或您需要替换帖子标题

标签: php wordpress wordpress-theming


【解决方案1】:
<?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,没有别的。
  • 没听懂,请解释一下
  • 谢谢,这很有帮助。如果有更多关于这两种方法( get_the_title() 与 the_title() )之间差异的信息,那就太好了。我研究了wordpress codex,似乎它们都返回了标题。为什么它在这种情况下有效?谢谢
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多