[英]How do you send a variable when including a php via shortcode?
我在這樣的WordPress頁面上包含一個PHP文件,如下所示: [include filepath='/my_file.php']
我更改為使其成為可能的functions.php
部分看起來像這樣:
function include_file($atts) {
extract(shortcode_atts(array('filepath' => NULL), $atts));
if ($filepath!='NULL' && file_exists( trailingslashit( get_stylesheet_directory() ) . $filepath)){
ob_start();
include(get_stylesheet_directory() . '/' . $filepath);
$content = ob_get_clean();
return $content;
}
}
但是,我希望能夠發送包含變量。 不可能這樣: [include filepath='/my_file.php?var=1']
,但也許您明白了。
現在,我創建了許多不同的.php文件,這些文件中都包含變量,並且像這樣包含它們: [include filepath='/my_file1.php'][include filepath='/my_file2.php']
這真令人討厭處理,如果我更改了原始php中的某些內容,則必須更改其他所有內容。 有沒有更好的辦法? :)
您可以在include語句之前定義一個變量,該變量對您包含的文件可見。
一個簡單的例子(我想將$foo
傳遞到我的文件中):
inc.php:
echo "inside inc.php '" . $foo . "' << ...\n";
test.php:
function hello()
{
$foo = 9999;
include('inc.php');
}
hello();
將輸出:
# php test.php
inside inc.php '9999' << ...
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.