【问题标题】:Getting a number instead of URL as output for the images with custom field in WP REST API Wordpress获取数字而不是 URL 作为 WP REST API Wordpress 中具有自定义字段的图像的输出
【发布时间】:2016-05-03 18:47:26
【问题描述】:

您好,我正在使用 WP Rest Api 插件来获取对我的自定义字段的 api 访问权限。 一切正常,除了图像的输出。我得到的是一个数字而不是一个 URL。

函数.php

    register_rest_field( 'team',
        'company_logo',
        array(
            'get_callback'    => 'slug_get_field',
            'update_callback' => null,
            'schema'          => null,
            )
        );
}

function slug_get_field( $object, $field_name, $request ) {
    return get_post_meta( $object[ 'id' ], $field_name, true );
}

company_logo 是图像的自定义字段,使用自定义内容类型管理器制作。

这是我以 JSON 形式检索的内容。

{
    "id": 10,
    "title": {
      "rendered": "Giel"
    },
    "content": {
      "rendered": ""
    },
    "company_logo": "16",
    "company_bg": "11"
}

请帮忙谢谢!

【问题讨论】:

    标签: wordpress rest


    【解决方案1】:

    我觉得你需要打电话

    wp_get_attachment_url() or wp_get_attachment_image_src()
    

    使用object['id'],因为这将返回完整的图片网址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多