How can I get json in my wordpress loop? Now I have this and it works fine, but very slow:
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $title = $post->post_title;
$context = stream_context_create(array('http' => array('ignore_errors' => true)));
if ($request = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=2b35547bd5675d8ecb2b911ee9901f59&artist='.urlencode($title).'', false, $context)) {
$xml = new SimpleXMLElement($request);?>
<img src="<?php if($xml->artist->image[3] != '') { echo $xml->artist->image[3]; } else { echo '/another-image.png'; } ?>"> // output image
<?php } endwhile;?>
Now I need to use ajax instead xml for get image url and I have request something like this. How can I insert this ajax output in my loop? Can someone help me?
$.ajax({
url: 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=<?php echo urlencode($title);?>&api_key=2b35547bd5675d8ecb2b911ee9901f59&format=json',
success: function(data) {
if(data.artist.image[3]["#text"] != '') {
// don't know how output data.artist.image[2]["#text"]
} else {
// output another image
}
},
})