【问题标题】:I have started building a small webapp and using JSON API on Wordpress to pull in wordpress posts onto my small webapp我已经开始构建一个小型 web 应用程序并在 Wordpress 上使用 JSON API 将 wordpress 帖子拉入我的小型 web 应用程序
【发布时间】:2014-07-22 09:10:05
【问题描述】:

所以就像我说的那样,我已经让它工作了,以便内容被拉入,但我也想知道它的发布日期。

<body style="background-color: white">

<script type="text/javascript">
$(document).ready(function () {
  var url = "http://wordpress.dev/?json=get_recent_posts";
  $.getJSON(url + "&callback=?", null, function(posts) {
      post_content = '';
      for(i in posts.posts) {
          post = posts.posts[i];
          post_content = post_content + "<div class='blue'>" + post.content + "</div><hr />";
      }
      $(".result").append(post_content);
  });
});
</script>

所以这就是我目前正在使用的,并且想知道如何才能让日期也显示在帖子中?

【问题讨论】:

  • 那是什么 JSON API?它是由某些插件提供的吗? (如果是this one,看起来日期就在post.date...)
  • 是的,我会在之前给它。
  • 鉴于您目前的要求,您最好使用仅从您的 WordPress 站点读取 RSS 提要的东西。有很多用于阅读 RSS 的 jQuery 插件/方法,并且不需要将任何插件添加到 WordPress — 您的 RSS 提要已经存在,以非常标准的形式内置在 WordPress 中,并为您提供包括其内容在内的最新帖子和日期...它是专门为提供听起来像您需要的东西而设计的,实际上...

标签: javascript php jquery wordpress


【解决方案1】:

我记得昨天和你说过,这里是其他人的相关帖子:I am building a small web application and i want to use JSON API to bring in Wordpress posts into my web app

如果您使用推荐的插件,您应该可以简单地使用post.date 来获取帖子的日期。你的代码:

<script type="text/javascript">
$(document).ready(function () {
  var url = "http://wordpress.dev/?json=get_recent_posts";
  $.getJSON(url + "&callback=?", null, function(posts) {
      post_content = '';
      for(i in posts.posts) {
          post = posts.posts[i];
          post_content = post_content + "<div class='blue'>" + post.content + " - "+ post.date +"</div><hr />";
      }
      $(".result").append(post_content);
  });
});
</script>

【讨论】:

  • 很好 :) 如果解决了,您能否将答案标记为已完成(勾号)?
【解决方案2】:

您可能只需要在输出中添加日期,因此将行更改为:

post_content = post_content + "<div class='blue'><span class='date'>" + post.date + "</span>" + post.content + "</div><hr />";

您还可以在 post-object 上使用其他属性,查看 http://wordpress.org/plugins/json-api/other_notes/#1.3.-Responses 以查看从 Service 返回的数据。

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 2011-06-02
    • 2016-10-07
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多