2014-06-19 66 views
1

我有一个使用自定义字段的主题。我想对做这样的事情json或xml在wordpress中的解析

我的自定义字段代码

<span> <?php echo get_post_meta($post->ID, 'rate', true); ?> </span> 

我有一个自定义字段命名为“速度”。我把IMDB id这样的字段这样

tt0068646 

我想从www.omdbapi.com获得imdb评级。我怎么能做到这一点?

回答

0

这个小函数将检索电影ID请求的结果,即http://www.omdbapi.com/?i=VALID-MOVIE-ID。其结果是在var $body

function get_imdb_json($id) 
{ 
    $url = "http://www.omdbapi.com/?i=$id"; 
    $response = wp_remote_get($url); 

    // Parse the request 
    if (! is_wp_error($response))//&& !isset($response['body']['error'])) 
    { 
     $body = json_decode(wp_remote_retrieve_body($response), true); 
     return '<pre>' . print_r($body, true) . '</pre>'; 
    } 
    return false; 
} 

而且,如果URL请求?imdb=MOVIE-ID替换后/页面内容使用的一个例子,即http://example.com/hello-world/?imdb=tt1217209

if(!is_admin()) 
{ 
    add_filter('the_content', function($content){ 
     if(isset($_GET['imdb']) && !empty($_GET['imdb'])) 
     { 
      if($ok = get_imdb_json($_GET['imdb'])) 
       return $ok; 
     }  
     return $content; 
    }); 
} 

而在你的情况下,将是一个问题:

echo get_imdb_json(get_post_meta($post->ID, 'rate', true));