首页 > 解决方案 > 用于网站图标和徽标图像的 Wordpress REST API

问题描述

有人可以帮助我如何在 Wordpress rest api 中获取徽标和网站图标图像的端点吗?当我使用 http:// .... / wp-json / 端点时,我得到的标题或名称和描述没有图像链接?

标签: wordpressrestapi

解决方案


对于徽标,您需要将此函数添加到functions.php:

// wordpress api for logo extension
add_action( 'rest_api_init', 'add_logo_to_JSON' );
function add_logo_to_JSON() {
register_rest_field( 'post', 'page_logo_src', array( // post for where to register - page_logo_src is the name for api
        'get_callback'    => 'get_logo_src',
        'update_callback' => null,
        'schema'          => null,
         )
    );
}

function get_logo_src( $object, $field_name, $request ) {
    $size = 'full';
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $feat_img_array = wp_get_attachment_image_src($custom_logo_id, $size, true);
    return $feat_img_array[0];
}

然后徽标在此处可用:http://your-domain/wp-json/wp/v2/postspage_logo_src.

您可以使用此功能获取所需的任何 wordpress 数据,以供 favicon 使用get_site_icon_url


推荐阅读