Wordpress 在单独的php文件中使用shortcode属性值

Wordpress 在单独的php文件中使用shortcode属性值,wordpress,Wordpress,如何在单独的php文件中使用shortcode属性值 我已经写了一个快捷代码,其中包含参数文件和类型 [includefile='membership-booking'mtype=“group”] 属性文件是固定的“会员预订”仅mtype可以是组或个人 现在在会员预订文件中,我想读取属性mtype值。基于mytype值,我需要在函数中回显一些内容,您可以像这样使用mtype $mtype = $data["mtype"]; 你可以全局使用这个变量,就像 $mtype = "post"; //

如何在单独的php文件中使用shortcode属性值 我已经写了一个快捷代码,其中包含参数文件和类型 [includefile='membership-booking'mtype=“group”]

属性文件是固定的“会员预订”仅mtype可以是组或个人


现在在会员预订文件中,我想读取属性mtype值。基于mytype值,我需要在函数中回显一些内容,您可以像这样使用
mtype

$mtype = $data["mtype"];
你可以全局使用这个变量,就像

$mtype = "post"; // This is global 

function shortcode_includefile( $atts){
  $data = shortcode_atts( array (
        'mtype' => 'post',
    ), $atts ) ;
  $mtype = $data["mtype"];
  $path = dirname(__FILE__) . "/shortcode_includefile/" . $atts['file']. ".php";  // You can use $mtype in this file
  if(file_exists($path))
    include $path;
  $code = ob_get_contents();
  ob_end_clean();
  return $code;

}

您可以在包含的文件中全局使用
$data[“mtype”]
。看到我的答案了吗?我已经声明了$mtype global,但是在这个函数之外,当我vardum p$mtype时,它显示为NULL,@RupaDas,因为您犯了一个很大的错误
$mtype
是本地作用域,这就是为什么会得到
NULL
。请参阅我的更新答案,
$mtype
现在是全局的
$mtype = "post"; // This is global 

function shortcode_includefile( $atts){
  $data = shortcode_atts( array (
        'mtype' => 'post',
    ), $atts ) ;
  $mtype = $data["mtype"];
  $path = dirname(__FILE__) . "/shortcode_includefile/" . $atts['file']. ".php";  // You can use $mtype in this file
  if(file_exists($path))
    include $path;
  $code = ob_get_contents();
  ob_end_clean();
  return $code;

}