Friday, April 10, 2015

WordPress JSON API to jQuery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>JSON Sample</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script type="text/javascript">
    var MYBLOG_LIMIT = 5;
    var MYWRAPPER_CLASS = 'wptitle';
    var MYWRAPPER_CLASS2 = 'wpimage';
    var MYWRAPPER_CLASS3 = 'wpdescription';
   
    var WP={open:function(b){var a={posts:function(){var d=MYBLOG_LIMIT;var e=0;var c={all:function(g){var f=b+"/api/get_recent_posts/";f+="?count="+d+"&page="+e+"&callback=?";jQuery.getJSON(f,function(l){var k=l.posts;for(var j=0;j<k.length;j++){var h=k[j];h.createComment=function(i,m){i.postId=h.id;a.comments().create(i,m)}}g(k)})},findBySlug:function(f,h){var g=b+"/api/get_post/";g+="?slug="+f+"&callback=?";jQuery.getJSON(g,function(i){h(i.post)})},limit:function(f){d=f;return c},page:function(f){e=f;return c}};return c},pages:function(){var c={findBySlug:function(d,f){var e=b+"/api/get_page/";e+="?slug="+d+"&callback=?";jQuery.getJSON(e,function(g){f(g.page)})}};return c},categories:function(){var c={all:function(e){var d=b+"/api/get_category_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.categories)})}};return c},tags:function(){var c={all:function(e){var d=b+"/api/get_tag_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.tags)})}};return c},comments:function(){var c={create:function(f,e){var d=b+"/api/submit_comment/";d+="?post_id="+f.postId+"&name="+f.name+"&email="+f.email+"&content="+f.content+"&callback=?";jQuery.getJSON(d,function(g){e(g)})}};return c}};return a}};
   
    var blog = WP.open('http://blogs.toledolibrary.org');
    blog.posts().all(function(posts){
      for(var i = 0; i < posts.length; i++){
        jQuery('.'+MYWRAPPER_CLASS).append(function(){
          return (posts[i].thumbnail) ? '<a target="_blank" class="lastpost_title" href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></a>' : '<a href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></a>';
           
           
        })
        jQuery('.'+MYWRAPPER_CLASS2).append(function(){
          return (posts[i].thumbnail) ? '<a href="'+posts[i].url+'"><img src="'+posts[i].attachments[0].images.full.url+'"/></a>'  : '<a href="'+posts[i].url+'"><img src="'+posts[i].attachments[0].images.full.url+'"/></a>';
           
           
        })
        jQuery('.'+MYWRAPPER_CLASS3).append(function(){
          return (posts[i].thumbnail) ? posts[i].excerpt : posts[i].excerpt;
           
           
        })
      }
    });
    </script>
    <style>
        .sociable { display:none; }
    </style>
</head>
<body>
   
    <div class="wptitle"></div>
    <div class="wpimage"></div>
    <div class="wpdescription"></div>
   
</body>
</html>

5 comments:

  1. I'm more than happy to find this page. I wanted to thank you for your time for this wonderful read!! I definitely enjoyed every bit of it and I have you bookmarked to check out new things in your informative web site.

    ReplyDelete