使用java(android应用程序)从wordpress类别帖子中提取(不使用wp模板)
我的情况-我想为android应用程序编写一个功能,使用java在wordpress站点的特定类别中提取最新帖子的使用java(android应用程序)从wordpress类别帖子中提取(不使用wp模板),java,android,wordpress,Java,Android,Wordpress,我的情况-我想为android应用程序编写一个功能,使用java在wordpress站点的特定类别中提取最新帖子的标题()和内容()字段。我不使用wordpress主题-我只是在我的网站的html中使用php插入,将各种wordpress项目放置在页面的不同位置 我还没有尝试过使用xml或rss,但在我看来,在这个过程中,似乎需要额外的步骤来做一些我想做的简单而简短的事情 使用java,我不能调用一个特定的html文件,该文件包含php插入,可以从wordpress数据库中提取我想要的项目-ht
标题()
和内容()
字段。我不使用wordpress主题-我只是在我的网站的html中使用php插入,将各种wordpress项目放置在页面的不同位置
我还没有尝试过使用xml或rss,但在我看来,在这个过程中,似乎需要额外的步骤来做一些我想做的简单而简短的事情
使用java,我不能调用一个特定的html文件,该文件包含php插入,可以从wordpress数据库中提取我想要的项目-html提取所有wordpress数据项目,java只是从html中提取字符串并显示在我的应用程序中吗?标题()
和内容()
都是php函数,因此,您不能从Java本身调用它们。您可以编写一个web服务,将这些值作为JSON返回,然后在Android应用程序中使用
// define hook for ajax functions
function core_add_ajax_hook() {
// don't run on admin
if ( !is_admin() ){
do_action( 'wp_ajax_' . $_REQUEST['action'] );
}
}
add_action( 'init', 'core_add_ajax_hook' );
// function to return latest title and content as JSON
function latest_post() {
// array for values
$json = array();
// get values for JSON array
if (have_posts()) : the_post();
// put values into JSON array
$json['the_title'] = get_the_title();
$json['the_content'] = get_the_content();
endif;
// encode to JSON and return
echo htmlentities(json_encode($json), ENT_NOQUOTES, 'UTF-8');
}
// hook function on request for action=latest_post
add_action('wp_ajax_latest_post', 'latest_post');
然后,您可以通过向以下机构发出请求来获取此信息: