2

我正在尝试使用javascriptWP API插件获取 WordPress 网站标题

我没有找到任何关于如何获取站点名称的示例,但我在 开发人员指南的实体部分下找到了变量名称

function _updateTitle(documentTitle) {
    document.querySelector('title').innerHTML = 
        documentTitle + ' | '+ $http.get('wp-json/name');
}

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

输出

有谁知道如何使用修复这个?

4

2 回答 2

2

你没有得到足够的上下文。是什么$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;
});
于 2015-07-05T16:39:06.063 回答
1

在此处查看更优雅的解决方案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/"}]}

于 2018-09-21T09:19:00.907 回答