如何从 WP API 获取站点名称

How to get site's name from WP API

我正在尝试使用 javascriptWP API 插件

我没有找到任何关于如何获取网站名称的示例,但我在 developer guide

的实体部分下找到了变量名称
function _updateTitle(documentTitle) {
    document.querySelector('title').innerHTML = 
        documentTitle + ' | '+ $http.get('wp-json/name');
}

$http.get('wp-json/name')的输出字符串为[object Object]

有谁知道如何解决这个问题?

您没有获得足够的上下文。 $http 是什么?当您直接在浏览器中转到 wp-json/name 时会发生什么?这是我看到的:

[{
  "code":"json_no_route",
  "message":"No route was found matching the URL and request method"
}]

这里有一个简单的例子可以让你获得标题:

var siteName;
$.getJSON( "/wp-json", function( data ) {
  siteName = data.name;
});

在这里查看更优雅的解决方案https://wordpress.stackexchange.com/a/314767/94636 响应不会包含额外的数据,例如: authentication: [] namespaces: ["oembed/1.0", "akismet/v1", "acf/v3", "wp/v2"] routes: {/: {namespace: "", methods: ["GET"],…},…} timezone_string: "" ... _links: {help: [{href: "http://v2.wp-api.org/"}]}