【发布时间】:2020-05-23 17:54:42
【问题描述】:
我的代码有点问题。我想插入带有自定义字段值的新帖子,但这些自定义字段不是来自 WordPress - 它们来自主题。简单的解决方案,例如:update_meta_value() 不起作用。我什至找不到为这些字段设置值的主题脚本。
我只发现了这么多代码,对我想要的领域有什么关注:
array(
'id' => 'movie_default_fields',
'type' => 'select',
'class' => 'chosen',
'title' => esc_html__('Enable Default Fields', 'amy-movie'),
'options' => array(
'movie_release' => esc_html__('Release Date', 'amy-movie'),
'movie_duration' => esc_html__('Duration', 'amy-movie'),
'movie_imdb' => esc_html__('IMDB', 'amy-movie'),
'movie_mpaa' => esc_html__('MPAA', 'amy-movie'),
'movie_language' => esc_html__('Language', 'amy-movie'),
'movie_genre' => esc_html__('Genre', 'amy-movie'),
'movie_actor' => esc_html__('Actor', 'amy-movie'),
'movie_director' => esc_html__('Director', 'amy-movie'),
),
'attributes' => array(
'multiple' => 'multiple',
),
'default' => array('movie_release', 'movie_duration', 'movie_imdb', 'movie_mpaa', 'movie_language', 'movie_genre', 'movie_actor', 'movie_director')
),
if (!empty($custom_fields)) {
foreach ($custom_fields as $field) {
if ($field['type'] == 'text') {
$name = (isset($field['name']) && $field['name'] != '') ? $field['name'] : '';
$movie_field[] = array(
'id' => sanitize_title($name),
'type' => 'text',
'title' => $name,
);
}
}
}
它显示特定自定义帖子类型中的自定义字段列表:
if (!function_exists('amy_movie_defaults_fields')) {
function amy_movie_defaults_fields() {
$default = array(
'movie_release',
'movie_duration',
'movie_imdb',
'movie_mpaa',
'movie_language',
'movie_genre',
'movie_actor',
'movie_director'
);
return $default;
}
}
if (in_array('movie_duration', $defaults_fields)) {
$movie_field[] = array(
'id' => 'movie_duration',
'type' => 'text',
'title' => esc_html__('Duration', 'amy-movie'),
'after' => '<em>min</em>',
);
}
我正在尝试在自动创建帖子时插入这些值,但它不起作用:
$post_id = wp_insert_post(array (
'post_type' => 'amy_movie',
'post_title' => $info->title,
'post_content' => $desc->description,
'post_status' => 'publish',
'movie_duration' => $info->duration, //simple example
));
【问题讨论】:
-
查看 wp_insert_post() 的文档:developer.wordpress.org/reference/functions/wp_insert_post 'movie_duration' 不是一个选项。您不能只为该功能弥补字段。你必须按照它的构建方式使用它。
-
除非您使用的主题重载了该功能。那我错了。
标签: php wordpress themes custom-fields