WordPress:无法访问回调函数中的回调参数键值
我正在开发一个简单的基于插件的自定义帖子类型。该职位类型注册良好。现在我想创建一些元框,并通过回调参数传递一些属性值。这就是我所尝试的:WordPress:无法访问回调函数中的回调参数键值,wordpress,custom-post-type,meta-boxes,Wordpress,Custom Post Type,Meta Boxes,我正在开发一个简单的基于插件的自定义帖子类型。该职位类型注册良好。现在我想创建一些元框,并通过回调参数传递一些属性值。这就是我所尝试的: function wpcd_add_dealer_meta_boxes() { add_meta_box( 'dealer_first_name', 'First Name', array($this, 'wpcd_meta_box_first_name_markup'),
function wpcd_add_dealer_meta_boxes() {
add_meta_box(
'dealer_first_name',
'First Name',
array($this, 'wpcd_meta_box_first_name_markup'),
'dealers',
'normal',
'default',
array(
'id' => 'first_name',
'name' => 'first_name',
'type' => 'text',
'placeholder' => 'Enter first name',
'maxlength' => '30',
'spellcheck' => 'true',
'autocomplete' => 'off'
)
);
}
这是我的回调函数和一个参数:
function wpcd_meta_box_first_name_markup($args) {
$html = '<input ';
$html.= 'type="' . $args->type . '" ';
$html.= 'id="' .$args->id . '" ';
$html.= 'name="' .$args->name . '" ';
if( isset($args->required) && ($args->required == 'true' || $args->required == '1' ) ) {
$html.= 'required ';
}
if( isset($args->placeholder) && $args->placeholder != '' ) {
$html.= 'placeholder="' . esc_attr( $args->placeholder ) . '" ';
}
if( isset($args->maxlength) ) {
$html.= 'maxlength="' . $args->maxlength . '" ';
}
if( isset($args->spellcheck) && ($args->spellcheck == 'true' ) ) {
$html.= 'spellcheck="' . $args->spellcheck . '" ';
}
if( isset($args->autocomplete) && ($args->autocomplete == 'on' ) ) {
$html.= 'autocomplete="' . $args->autocomplete . '" ';
}
$html.= '/>';
echo $html;
}
而实际输出是
<input type="" id="" name="" />
属性type
、id
和name
没有包装在if(isset())
块中,因此它们被生成(带有空值),而在if(isset())
块中包装的任何内容都被忽略,就好像它们根本没有被设置一样
我错过了什么或做错了什么?
任何建议都可以挽救我的生命。如果你仔细检查一下,你会看到:
($callback_args(数组)(可选)应设置为
框数组的$args属性(它是传递的第二个参数
回拨电话)
传递给回调函数的第一个参数是WP\u Post
对象。第二个是参数数组。因此,请尝试:
function wpcd_meta_box_first_name_markup($post, $args) { ...
然后按预期访问您的参数:
$args['type']
如果仔细检查,您将看到:
($callback_args(数组)(可选)应设置为
框数组的$args属性(它是传递的第二个参数
回拨电话)
传递给回调函数的第一个参数是WP\u Post
对象。第二个是参数数组。因此,请尝试:
function wpcd_meta_box_first_name_markup($post, $args) { ...
然后按预期访问您的参数:
$args['type']
使用
$args['type']
等如何?确实如此。您正在将值放入数组中,并使用对象访问语法尝试将其取出。@Und3rTow我尝试了$args['type']
已经得到了以下信息:致命错误:无法在第68行的/var/www/WP projects/subratasarkar.com/WP-content/plugins/product-dealer/product-dealer.php中使用WP\u Post-as数组类型的对象
@MattGibson您能告诉我这样做的正确方法吗?使用$args['type']
等等?确实如此。您正在将值放入数组中,并使用对象访问语法尝试将其取出。@Und3rTow我尝试了$args['type']
已经得到了以下信息:致命错误:无法在/var/www/WP projects/subratasarkar.com/WP-content/plugins/product-dealer/product-dealer.php第68行@MattGibson上使用WP\u Post类型的对象作为数组。请告诉我正确的方法好吗?是的,我终于找到了它。我缺少了第一个参数$Post
。然后使用var_dump,我了解了数组是如何形成的。很抱歉,先生。非常感谢您的研究。是的,我终于让它工作了。我丢失了第一个参数$post
。然后使用var_dump,我了解了数组是如何形成的。很抱歉,先生。非常感谢您的研究信息技术