我如何开发一个WordPress插件来获取帖子并在主页上将其解析为JSON


How do I develop a WordPress plugin to fetch the posts and parse it as JSON on my home page?

我正在尝试开发一个小型的WordPress插件,从网站上获取帖子和页面,并将其解析为json,以便在移动应用程序中进一步使用。现在我正在通过这种方法实现目标:

1) 创建了一个关于我当前活动主题的文件webservice.php,例如Twentythrteen。所以文件的位置是:

http://www.example.com/wp-content/themes/twentythirteen/webservice.php

2) 我在URL上发布参数,以获得类似的JSON响应

http://www.example.com/wp-content/themes/twentythirteen/webservice.php?type=page&limit=10

问题是我想在主页上发布这样的参数:

http://www.example.com?type=page&limit=10 

我不知道怎么做,但我看到了JSON API插件,它也在做同样的事情,但我无法在代码中找到它是如何从主页获取请求并在同一页面上解析JSON的。我该怎么做?

我开发了一个WordPress插件,并将其用于我的PhoneGap应用程序,但它也可能对您有所帮助。这是回调最后一篇文章的代码:

header('Content-Type: application/json');
require('../../../wp-load.php');
require('../../../wp-includes/pluggable.php');
$post = "";
$elementos = 5; //Number of Post
$yaCargados = 0;
global $wpdb;
if($_POST['num_post']!=0 or $_POST['num_post']!="NULL") {
$elementos = $_POST['num_post'];
$yaCargados = $_POST['paginacion'];
}
$args = array(
    'posts_per_page'    => $elementos,
    'offset'           => $yaCargados,
    'orderby'          => 'post_date',  
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$posts_array = get_posts( $args );
if(0 < $posts_array) {
foreach( $posts_array as $term) {
$res['posts'][] = $term;    
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $term->ID ), 'medium' );
$res['images'][]['imagen'] = $image;
$custom_fields = get_post_custom($term->ID);
 $res['custom_field'][] = $custom_fields;
}
echo json_encode($res);
}else{
}

/wp-content/plugins/[YOUPLUGIN]保存存档,并以JSON格式调用和打印帖子。

快乐编码!