使用帖子类型填充ACF复选框

时间:2016-03-14 20:18:03

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

我尝试使用WP网站的各种帖子类型填充复选框ACF字段。这是一个插件,所以帖子类型会根据安装位置而有所不同。

默认情况下,插件使用网页和帖子作为帖子类型,但需要提供用户选项以使用复选框选择网站上的其他CPT。如何在复选框字段中填入网站上所有CPT的列表。这是我目前在插件中加载字段的PHP代码部分

array (
    'key' => 'field_56e6d87b6c7be',
    'label' => 'Add to Custom Post Types',
    'name' => 'fb_pixel_cpt_select',
    'type' => 'checkbox',
    'instructions' => 'Select which Custom Post Types you would like to use.',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => array (
        'width' => '',
        'class' => '',
        'id' => '',
    ),
    'choices' => array (
    ),
    'default_value' => array (
    ),
    'layout' => 'vertical',
    'toggle' => 0,
),

2 个答案:

答案 0 :(得分:2)

您可以在此处使用ACF加载字段功能自动填充您的字段。点击此处:http://www.advancedcustomfields.com/resources/acfload_field/

然后,使用Wordpress get_post_types调用(https://codex.wordpress.org/Function_Reference/get_post_types),您可以检索这些值并像这样填充您的字段。

add_filter('acf/load_field/name=fb_pixel_cpt_select', 'acf_load_post_types');

function acf_load_post_types($field)
{
    foreach ( get_post_types( '', 'names' ) as $post_type ) {
       $field['choices'][$post_type] = $post_type;
    }

    // return the field
    return $field;
}

在尝试填充之前,请确保您的字段已经创建。

答案 1 :(得分:1)

这将为您的ACF块或具有所有帖子类型的任何内容(不具有导航菜单的内容,例如附件帖子类型)创建一个选择字段。

int setId = -1;
var picIndex = -1;
foreach (var set in lstSets)
{
    picIndex = set.Pictures.FindIndex(p => p.Id == picToFind.Id && p.Name == picToFind.Name);
    if (picIndex > -1)
    {
        setId = set.Id;
        break;
    }
}

enter image description here

相关问题