2015-04-05 51 views
1

內我在WordPress的子主題,我想在每一個帖子的末尾插入此代碼:插入PHP函數

<?php the_tags(); ?> 

我不想要編輯的single.php。我想通過的functions.php

插入此代碼,我發現這個線程和使用這個答案代碼:https://wordpress.org/support/topic/insert-info-into-bottom-of-posts#post-3990037

它的工作很適合我,但我無法弄清楚如何插入我的PHP代碼。

這些都是我試過,但沒有反映什麼,我在前端想要的東西:

$content.= '<?php the_tags(); ?>'; 

$content.= ' the_tags();'; 

$content.= <?php the_tags(); ?>; 

$content.= the_tags(); 

我如何改變在WordPress的線程的代碼,包括PHP?

謝謝。

回答

0

您正試圖將the_tags的輸出連接到$content,但the_tags不返回任何內容。當您致電the_tags時,它會將輸出發送到瀏覽器本身。

the_tags幾乎只是一個圍繞get_the_tag_list的包裝,它將內容作爲字符串返回而不是輸出。嘗試:

$content .= get_the_tag_list(); 

而且,我只想澄清究竟什麼是錯的你的一些嘗試:

$content.= <?php the_tags(); ?>; 

<?php基本上意味着開始解釋PHP,在這一點上我假設你已經打開您的PHP標籤,因此不需要再次執行,試圖這樣做會導致錯誤。

$content.= ' the_tags();'; 

這將文本字符串the_tags();追加到$content。你不能像這樣將函數調用嵌入到字符串中。

$content.= '<?php the_tags(); ?>'; 

這最後一行只是我剛剛提到的兩個問題的組合。這將導致文字字符串<?php the_tags(); ?>被追加到$content

+0

你好。感謝解釋爲什麼我的代碼錯了!我對學習PHP如何工作非常感興趣。我試着輸入$ content。= get_the_tag_list();它似乎沒有奏效。我也試過$ content。= get_the_tags();那也沒用。有任何想法嗎? – Tara 2015-04-05 02:32:23

+0

@Tara你能用更多的代碼更新你的問題嗎? – 2015-04-05 02:33:42

+0

哦,我的天哪。當我發佈SE之前,我意識到我已經註釋了我的代碼,因此我不想在前端反映任何內容。你的代碼工作!非常感謝。 – Tara 2015-04-05 02:35:37