当我尝试为面向对象方法创建循环时(我想),php会提示我两个错误。我认为它必须做一些事情($args
) 不能用于我应该注入字符串的地方,但我找不到答案。我应该在foreach中运行foreach循环吗?
This is the case:
我有一个文件名为
themeScripts.php 其中我返回
array();
像这样:
return array(
\'custom\' => array(
\'src\' => \'/includes/js/custom.js\',
\'deps\' => \'jquery\',
\'ver\' => \'4.0\',
\'in_footer\' => true
),
\'selecter\' => array(
\'src\' => \'/includes/js/jquery.fs.selecter.min.js\'
\'deps\' => \'jquery\'
\'ver\' => false,
\'in_footer\' => true
)
);
在此之后,我想运行foreach循环,以便定义函数的循环
wp_enqueue_script
像这样:
public function loadScripts() {
// Include themeScripts from config
$themeScripts = include(dirname(__FILE__) . "/../config/themeScripts.php");
// Loop trough each themeScript
foreach($themeScripts as $handle => $args):
wp_enqueue_script( $handle, $args);
endforeach;
}
警告:preg\\u match()要求参数2为字符串,数组在/Users/username/Sites/sitename/public\\u html/themename/wp includes/class中给定。wp脚本。php在线128
警告:strpos()要求参数1为字符串,数组在/Users/username/Sites/sitename/public\\u html/themename/wp includes/class中给出。wp脚本。php在线128
SO网友:Shazzad
wp_enqueue_script 将参数类型分别声明为string - string - array - string - boolean
. 将数组作为第二个参数传递时,会发生错误。
现在,比较$数组数据,您可以通过提取数组或使用数组键/值来处理它-
public function loadScripts()
{
$themeScripts = iarray(
\'custom\' => array(
\'src\' => \'/includes/js/custom.js\',
\'deps\' => \'jquery\',
\'ver\' => \'4.0\',
\'in_footer\' => true
),
\'selecter\' => array(
\'src\' => \'/includes/js/jquery.fs.selecter.min.js\'
\'deps\' => \'jquery\'
\'ver\' => false,
\'in_footer\' => true
)
);
foreach( $themeScripts as $handle => $args ):
extract($args);
wp_enqueue_script(
$handle,
$src,
array($deps),
$ver,
$in_footer
);
endforeach;
}