[英]Why can't I pass array item into my function with php?
My items from my array seem to not be passed into the my 'resolve' => function.我的数组中的项目似乎没有传递到我的 'resolve' => 函数中。 Right now the function returns fail.
现在函数返回失败。 If I replace $meta with an array item then it works.
如果我用数组项替换 $meta ,那么它就可以工作。 Im trying to fetch meta values for GraphQL and dont want to make a function per field.
我试图为 GraphQL 获取元值,并且不想为每个字段创建一个函数。
add_action( 'graphql_register_types', function() {
$metas = array('phone', 'city', 'state', 'zip');
foreach($metas as $meta){
register_graphql_field( 'Location', $meta, [
'type' => 'String',
'description' => __( 'The post data', 'wp-graphql' ),
'resolve' => function($post, $meta) {
$GQL_data = get_post_meta( get_the_ID(), $meta, true);
return ! empty( $GQL_data ) ? $GQL_data : 'fail';
}
] );
}
});
Expected GraphQL output:预期的 GraphQL 输出:
"phone": "123-123-1234", "电话": "123-123-1234",
"city": "Los Angeles", "city": "洛杉矶",
"state": "CA", "状态": "CA",
"zip": "99922" “邮编”:“99922”
currently all items have a value of: "fail"当前所有项目的值为:“失败”
This is a callback anonymous function, you cant just change its parameters.这是一个回调匿名函数,你不能只改变它的参数。 What you can use is 'use' like documented here https://secure.php.net/manual/en/functions.anonymous.php
您可以使用的是“使用”,如此处记录的https://secure.php.net/manual/en/functions.anonymous.php
register_graphql_field( 'Location', $meta, [
'type' => 'String',
'description' => __( 'The post data', 'wp-graphql' ),
'resolve' => function($post) use ($meta) {
$GQL_data = get_post_meta( get_the_ID(), $meta, true);
return ! empty( $GQL_data ) ? $GQL_data : 'fail';
}
] );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.