【发布时间】:2018-06-30 13:19:15
【问题描述】:
我想在我的 Wordpress 帖子中添加一个“帖子作者”选择选项。我不想在 Wordpress 中创建 30 个左右不同的用户,而是想用自定义帖子类型(员工)的所有标题填充 ACF 下拉选择字段。
我发现此代码用于输出自定义帖子类型标题列表...
// query for your post type
$post_type_query = new WP_Query(
array (
'post_type' => 'your-post-type',
'posts_per_page' => -1
)
);
// we need the array of posts
$posts_array = $post_type_query->posts;
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );
...我从一篇关于动态填充选择框的 ACF 文章中找到了这段代码...
function acf_load_color_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_field('my_select_values', 'option', false);
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');
...但是我真的不确定如何将两者拼接在一起,以便它获取我的自定义字段标题并将它们添加到该 ACF 字段选择器。
我尝试了,但无法显示任何结果。
有什么想法吗?
【问题讨论】:
-
使用 ACf,您可以创建一个类型为“relation”的字段,然后在下面选择自定义帖子类型。
标签: wordpress custom-post-type advanced-custom-fields