jackrabbit-d
10/18/2017 - 1:34 PM

wpjson random on refresh

jquery required prerequisites: A wordpress install with RestAPI, this plugin or equivalent https://wordpress.org/plugins/acf-to-rest-api/ https://github.com/airesvsg/acf-to-rest-api

jQuery(function($){
    var fact = ''; // Content Var
    var imgIdUrl = ''; //For featured image feed
    var imgUrl = ''; // For featured image url
   $.getJSON('example.com/wp-json/wp/v2/fact-post', function(data){
       var item = data[Math.floor(Math.random()*data.length)]; // Random Array Item
       fact = item.content.rendered;
       imgIdUrl = 'example.com/wp-json/wp/v2/media/'+item.featured_media;
       console.log(item);
   }).done(function() {
       $('#element').html(fact); 
       $.getJSON(imgIdUrl, function(data){
           imgUrl = data.media_details.sizes.full.source_url;
           console.log(imgUrl);
       }).done(function() {
           $('#element-img').attr('src', imgUrl);
       });
   });
});