2014-02-19 68 views
1

我想從WordPress的post.I的文本編輯器中添加簡碼添加按照wordpress的帖子短代碼:短碼WordPress的文本編輯器不工作

<img alt="" src="[template_url]/images/ic_sol_1a.png" />

這裏[template_url]是短碼我想用它沒有工作。當我在帖子頁面看到它時,它顯示的文字不是短代碼響應。某處我看到了一個解決方案,如添加下面的行添加到functions.php主題:

add_filter('widget_text', 'shortcode_unautop');

add_filter('widget_text', 'do_shortcode');

但還是加上幾行後,我無法讓簡碼的工作。可能的原因是什麼?

我的簡碼的功能是這樣的:

function my_template_url() { 
    return get_bloginfo('template_url'); 
} 
add_shortcode("template_url", "my_template_url"); 
+0

你在哪裏放置短代碼功能?這是否在你的主題的functions.php文件中?你提到這是在WordPress的文章的文本編輯器,所以你顯示的過濾器不適用。你在輸出哪個文件?你可以包含你的模板標記這個特定的部分? –

+0

@NathanDawson我在theme.php裏添加了短代碼函數,並在創建帖子的時候在文章編輯器中使用了短代碼[[template_url]''。 –

+0

請包括single.php –

回答

1

經過很多頭痛,並在@doublesharp和@Nathan Dawson評論/答案的幫助下,我們發現問題出在single.php主題文件中,我們使用get_the_content函數獲取帖子內容。通過將其更改爲the_content()函數sortcodes開始工作。 請注意短代碼在管理站點上的WordPress發佈的可視化編輯器中不起作用,但當您通過轉到該帖子實際在瀏覽器中看到該帖子時,您可以看到它工作。

+1

以下添加正確的答案我很高興聽到您解決這個問題,但請回到原點。 #1是錯誤的。簡碼只能返回一個值,永遠不會回顯。最後一段在筆記下:https://codex.wordpress.org/Function_Reference/add_shortcode#Notes –

+0

@NathanDawson好吧,我會刪除那一點 –

2

您將需要使用get_bloginfo()返回值作爲你的榜樣,並確保使用the_content()的值打印頁面。如果您使用其中一種API方法(如get_the_content()),則do_shortcode過濾器不會運行。如果要應用它,請使用以下內容:

function my_template_url() { 
    // This will echo the 'template_url' 
    return get_bloginfo('template_url'); 
} 
add_shortcode("template_url", "my_template_url"); 

$shortcoded = apply_filters('the_content', get_the_content()); 

您不需要add_filter()行。

+0

你的邏輯是正確的。我試圖按照你說的方式來實現它,但是無法使其它任何詞彙變成現實? –

+0

感謝您的幫助,我們發現了這個問題。你的邏輯也幫助了很多。我會在 –

相關問題