0

My situation - I want to write a feature for an android app using java to pull the the_title() and the_content() fields of the latest post in a specific category of a wordpress site. I don't use wordpress themes - I just use php inserts in my site's html to place various wordpress items in various places on the page.

I have not tried using xml or rss yet, but it seems to me to put extra steps in the process to do something as simple and short as what I want to do.

Using java, can't I call to a specific html file that has php inserts that pull the items I want from the wordpress database - the html pulls all the wordpress data items and the java simply pulls the strings from the html and show in my app?

athspk
  • 6,722
  • 7
  • 37
  • 51

1 Answers1

0

the_title() and the_content() are PHP functions, so you can't call them from Java per se. You could write a web service that returns these values as JSON, which you can then use in your Android app.

// 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');

You can then get this info by making a request to:

http://yoursite.com/wp-load.php?action=latest_post

doublesharp
  • 26,888
  • 6
  • 52
  • 73