Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何开发一个WordPress插件来获取帖子并在我的主页上将其解析为JSON?_Php_Wordpress_Mobile Application_Json Api_Jsonresponse - Fatal编程技术网

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

Php 我如何开发一个WordPress插件来获取帖子并在我的主页上将其解析为JSON?,php,wordpress,mobile-application,json-api,jsonresponse,Php,Wordpress,Mobile Application,Json Api,Jsonresponse,我正在尝试开发一个小的WordPress插件,从网站获取帖子和页面,并将其解析为json,以便在移动应用程序中进一步使用。 现在,我正通过以下方法实现目标: 我在当前活动主题(如Twenty13)上创建了一个文件webservice.php。因此,文件的位置是: http://www.example.com/wp-content/themes/twentythirteen/webservice.php 2我在该URL上发布参数以获得如下JSON响应 http://www.example.com

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

我在当前活动主题(如Twenty13)上创建了一个文件webservice.php。因此,文件的位置是:

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格式调用和打印帖子

快乐编码