2016-05-26 34 views
3

我想替換wp標題中的文字,
其實,有一個流行文章的小部件。
我要替換這個小部件的輸出文本,所以我看了看周圍的小部件的功能,我發現這個代碼:如何在wordpress the_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... 

我一直在努力,這條線<?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; 
?> 
+0

the_title替換()函數用於顯示文章標題和那個受歡迎的帖子插件也顯示來自帖子部分的熱門帖子ñ所以如果你想改變某些東西或想添加你可以追加到the_title()或者你需要替換帖子標題 –

回答

1

試試這個

$wptitle = get_the_title(get_the_ID()); 
$wptitle = str_replace('textbefore', 'textafter', $wptitle); 
echo $wptitle; 
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,只有1,沒有別的。 –

+0

沒有得到你,請解釋 –

+0

謝謝,這是有幫助的。如果有更多關於這兩種方法(get_the_title()vs the_title())差異的信息將會很好。我研究了wordpress codex,看起來他們都返回標題。爲什麼它在這種情況下工作?謝謝 – FNunez