2014-02-21 50 views
0

我有一個封閉的小部件,可以在發佈/頁面中正常工作,但是在文本小部件中它不會在短代碼標籤之間傳遞內容。文本小部件中的WordPress短代碼不通過內容

短碼的樣子:

[wpbutton]wpbutton[/wpbutton]

而且代碼:

add_shortcode('wpbutton', array($this, 'shortcode')); 
add_filter('widget_text', array($this, 'shortcode')); 

function shortcode($args, $content = null) { 

    extract(shortcode_atts(array(
     'action' => '', 
     'classes' => 'wpbutton', 
    ), $args)); 

    echo wpbutton($action, $content, $classes); 

    // Added for testing - echos in sidebar! 
    if (!$content) { 
     echo 'no content'; 
    } 

} 

function wpbutton($action, $content, $classes) { 
    // Do stuff 
} 

在後期製作/網頁內容它回聲任何標記之間,但在文本組件它回聲報「沒有內容「

任何人都知道如何解決?

回答

0

我想你應該改變你的過濾功能,以這樣的:

add_filter('widget_text', array($this, 'do_shortcode')); 

代替

add_filter('widget_text', array($this, 'shortcode')); 

它通過文本組件的WordPress的的do_shortcode()功能的內容,而不是你的功能。 Wordpress函數將自行處理簡碼,並自動調用您的shortcode函數。

如所見herehere

+0

非常多。 'array($ this,'function')'格式用於對象內部時(我從這個問題的OO內容中取出代碼),所以答案是使用'add_filter('widget_text','do_shortcode'); 。謝謝! –

相關問題