使用自定义帖子类型的标题列表作为ACF字段

时间:2018-01-21 23:00:37

标签: wordpress custom-post-type advanced-custom-fields

我想添加一个' Post Author'在我的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字段选择器。

我试过但无法显示任何结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

感谢mmmm的评论,我能够在ACF中使用'关系'字段类型来实现我想要的...这是我感兴趣的人的代码

'post-author'是ACF字段的名称,已设置为关系,并且从选项中选择了自定义帖子类型'staff',因此我可以选择任何员工。然后在帖子中输出缩略图和职称......

<?php

$posts = get_field( 'post-author' );

if ( $posts ): ?>

<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>

<div class="author">
<?php the_post_thumbnail('small'); ?>
<div class="author-text">
    <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
    </a>
    <p>
        <?php the_field('job_title') ?>
    </p>
</div>
<div class="clearfix"></div>
</div>

<?php endforeach; ?>

<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
相关问题