2014-02-12 22 views
1

如何使用全局變量在WordPress使用do_shortcode

$a = 'blablablablabla'; 
function Test($atts, $content = null){ 
    global $a; 
    return '<p>'.$a.'</p>'; 
} 
add_shortcode('test', 'Test'); 
$x = do_shortcode($content); 
remove_shortcode('test'); 

return $x; 

但測試沒有得到這個地方的全局變量,我認爲這是因爲「do_shortcode」,我怎樣才能得到一個全局變量來自外部的功能?

+0

把它作爲一個函數的參數? –

+0

你是什麼意思? –

回答

2

試試這個代碼這是在我的wordpress上工作。

$GLOBALS['a'] = 'blablablablabla'; 
function Test($atts, $content = null){ 
    return '<p>'.$GLOBALS['a'].'</p>'; 
} 
add_shortcode('test', 'Test'); 

$含量= '[測試]';

$x = do_shortcode($content); 
remove_shortcode('test'); 
echo $x; 
exit; 

的出看跌期權

blablablablabla 
+0

你沒有改變任何東西,我相信do_shortcode正在運行,測試正在執行,我不知道爲什麼你去好,對我沒有。 –

+0

@BlackCid我只是說不給do_shortcode一個內容。只是簡單地測試你的短代碼名稱。所以do_shortcode是明白,運行測試功能,否則不運行任何事情。 –

+0

問題不在於函數Test的執行,問題在於我的wordpress中,$ a是空的函數,Test不是全局值... –