簡體   English   中英

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

[英]How to use global variables in WordPress using 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;

但是Test沒有得到這個地方的全局變量,我想是因為“ do_shortcode”,我怎么能從函數外部獲得全局變量?

試試這個代碼,這是我的WordPress上的工作。

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

$ content ='[測試]';

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

輸出是

blablablablabla

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM