简体   繁体   English

使用WP REST API设置ACF

[英]Setting up ACF with WP REST API

I am using the following plugins to get JSON data from WordPress REST API: 我使用以下插件从WordPress REST API获取JSON数据:

Advanced Custom Fields PRO WP REST API ACF to REST API 高级自定义字段PRO WP REST API ACF到REST API

I have a created custom post type called "Custom Navigation". 我有一个名为“自定义导航”的自定义帖子类型。 I have a custom field group (through ACF) called "Custom Navigation Item" that is only applied when the post type is Custom Navigation. 我有一个名为“自定义导航项”的自定义字段组(通过ACF),仅在帖子类型为自定义导航时应用。 The fields in this group are called "Image" and "Caption", which I can't get the values for. 该组中的字段称为“图像”和“标题”,我无法获取其值。

Here is where I set my menus and add filters for the ACF to REST API plugin. 这是我设置菜单并为ACF添加REST API插件的过滤器的地方。 These work as intended. 这些按预期工作。

function custom_setup() {
    // This theme uses wp_nav_menu() in two locations.
    register_nav_menus( array(
        'topnav'    => 'Top Menu',
        'footer-left-nav' => 'Footer Top Menu 1',
        'footer-center-left-nav' => 'Footer Top Menu 2',
        'footer-center-right-nav' => 'Footer Top Menu 3',
        'footer-right-nav' => 'Footer Top Menu 4',
        'footer-bottom-left-nav' => 'Footer Bottom Menu 1',
        'footer-bottom-right-nav' => 'Footer Bottom Menu 2',
    ) );

    // Enable the option show in rest
    add_filter( 'acf/rest_api/field_settings/show_in_rest', '__return_true' );

    // Enable the option edit in rest
    add_filter( 'acf/rest_api/field_settings/edit_in_rest', '__return_true' );
}

add_action( 'after_setup_theme', ‘custom_setup' );

This function sets my custom post type menu items to the WP REST API. 此函数将我的自定义帖子类型菜单项设置为WP REST API。 This works as intended in so far as doing this. 这样做就像预期的那样。 The ACF fields of these post type menu items however do not send 但是,这些发布类型菜单项的ACF字段不发送

function wp_api_v2_custom_get_menu_data ($data) {

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[$data['id']] );
    $menuItems = wp_get_nav_menu_items($menu->term_id);

    return $menuItems;
    } else {
        return array();
    }
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z(-]+)', array(
        'methods' => 'GET',
        'callback' => 'wp_api_v2_custom_get_menu_data',
    ) );
} );

Here is my function that sets my custom post type 这是我的功能,设置我的自定义帖子类型

function custom_nav_post_type() {
    register_post_type(‘custom_nav',
        array (
            'name'             => ‘Custom Navigation',
            'singular_name'        => 'CustomNav',
            'can_export'        => TRUE,
            'exclude_from_search'    => FALSE,
            'has_archive'        => TRUE,
            'hierarchical'        => TRUE,
            'label'            => 'Custom Navigation',
            'menu_position'        => 5,
            'public'            => TRUE,
            'publicly_queryable'    => TRUE,
            'query_var'        => ‘customnav',
            'rewrite'            => array ( 'slug' => 'shop' ),
            'show_ui'        => TRUE,
            'show_in_menu'    => TRUE,
            'show_in_nav_menus'    => TRUE,
            'show_in_rest'        => TRUE,
            'rest_base'        => 'shop',
                'rest_controller_class'    => 'WP_REST_Posts_Controller',
            'supports'        => array ('title')
        )
    );
}
add_action( 'init', 'custom_nav_post_type' );
?>

The output from this all of this 所有这一切的输出

[
{
    "id":184,
    "acf":[

    ]
},
{
    "id":183,
    "acf":[

    ]
},
{
    "id":182,
    "acf":[

    ]
},
{
    "id":181,
    "acf":[

    ]
},
{
    "id":180,
    "acf":[

    ]
},
{
    "id":176,
    "acf":[

    ]
}
]

Any advice on where I am going wrong would be greatly appreciated. 任何关于我出错的建议都将不胜感激。

Here is how I solved it. 这是我如何解决它。 I was missing a filter for my custom post type which I added to my "custom_setup" function. 我错过了我的自定义帖子类型的过滤器,我添加到我的“custom_setup”函数中。

add_filter('rest_prepare_custom_nav', function($response) {
     $response->data['acf'] = get_fields($response->data['id']);
     return $response;
});

So the filter is a wild card filter, applied like 所以过滤器是一个外卡过滤器,应用如

apply_filter("rest_prepare_YOUR-CUSTOM-POST-TYPE-NAME-HERE", ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM