Uncaught TypeError: Cannot access type of string on string in C: opspanel domainswordpresswp-contentpluginsadvanced-custom-fields-proincludesacf-value-functions.php:63 Stack trace: #0
编写这个函数:
function acf_get_value( $field, $post_id = 0 ) {
// Allow filter to short-circuit load_value logic.
$value = apply_filters( "acf/pre_load_value", null, $post_id, $field );
if( $value !== null ) {
return $value;
}
// Get field name.
$field_name = $field['name']; // --- HERE ERROR ---
// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name" ) ) {
return $store->get( "$post_id:$field_name" );
}
// Load value from database.
$value = acf_get_metadata( $post_id, $field_name );
// Use field's default_value if no meta was found.
if( $value === null && isset($field['default_value']) ) {
$value = $field['default_value'];
}
/**
* Filters the $value after it has been loaded.
*
* @date 28/09/13
* @since 5.0.0
*
* @param mixed $value The value to preview.
* @param string $post_id The post ID for this value.
* @param array $field The field array.
*/
$value = apply_filters( "acf/load_value", $value, $post_id, $field );
// Update store.
$store->set( "$post_id:$field_name", $value );
// Return value.
return $value;
}
$字段= get_field_object("名字");
field_name =美元领域['名字'];
不工作
此错误表示您正在尝试使用不存在的键从关联数组中获取值。
根据您识别的行,将代码更改为:
// Get field name.
$field_name = $field['name'] ?? '';
如果你使用的是低于7的php版本,将该行改为
$field_name = $field['name'] ?: '';
但是,就像第一条注释建议的那样,执行var_dump($field)来查看数组的内容。上面的代码只会设置一个默认值,而不会给出致命错误。