【问题标题】:Setting up ACF with WP REST API使用 WP REST API 设置 ACF
【发布时间】:2017-08-20 19:59:33
【问题描述】:

我正在使用以下插件从 WordPress REST API 获取 JSON 数据:

高级自定义字段专业版 WP REST API ACF 到 REST API

我创建了一个名为“自定义导航”的自定义帖子类型。我有一个名为“自定义导航项”的自定义字段组(通过 ACF),仅在帖子类型为自定义导航时应用。该组中的字段称为“图像”和“标题”,我无法获取它们的值。

我在这里设置菜单并为 ACF 到 REST API 插件添加过滤器。这些按预期工作。

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' );

此函数将我的自定义帖子类型菜单项设置为 WP REST API。就这样做而言,这按预期工作。但是这些帖子类型菜单项的 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',
    ) );
} );

这是我设置自定义帖子类型的函数

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' );
?>

这一切的输出

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

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

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

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

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

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

    ]
}
]

任何关于我哪里出错的建议将不胜感激。

【问题讨论】:

  • 请考虑最小化您的代码以适应MCVE statement
  • 已编辑,希望能更清楚一点,抱歉

标签: php wordpress rest advanced-custom-fields


【解决方案1】:

这是我解决它的方法。我错过了我添加到“custom_setup”函数中的自定义帖子类型的过滤器。

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

所以过滤器是一个通配符过滤器,应用类似于

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

【讨论】:

  • 您好@Allan Fitzpatrick,为什么“...”请完成代码 sn-p。谢谢
猜你喜欢
  • 2022-11-26
  • 2018-03-23
  • 2017-09-12
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 2019-10-26
相关资源
最近更新 更多