Ejemplo n.º 1
0
/**
 * Api method for retrieving posts
 *
 * @param string $page 
 * @param string $posts_per_page 
 * @return void
 * @author Armando Sosa
 */
function api_get_post($id)
{
    global $api;
    $posts = array();
    // query parameters
    $options = array('post_status' => 'publish', 'p' => $id);
    if ($posts_per_page > 0) {
        $options['posts_per_page'] = $posts_per_page;
    }
    // query for the posts
    query_posts($options);
    global $wp_query;
    $max_pages = $wp_query->max_num_pages;
    // this is the meta information that we'll pass to the app
    $object['meta'] = array('max_pages' => $max_pages, 'current_page' => absint(get_query_var('paged')));
    // loop trough the posts and propagate the object array
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            // this is a hack to get full posts always
            global $more, $post;
            $more = true;
            // this is to get the content with all the filters without echoing it.
            $content = str_replace(']]>', ']]>', apply_filters('the_content', get_the_content('')));
            // populate the object;
            $postObject = array('id' => $post->ID, 'author_id' => $post->post_author, 'author_display_name' => get_the_author(), 'title' => get_the_title(), 'post_date' => $post->post_date, 'permalink' => get_permalink(), 'content' => $content, 'excerpt' => get_the_excerpt(), 'featured_image' => PostsHelper::getImage(), 'comments_open' => comments_open($post->ID), 'comment_count' => get_comments_number(), 'shortlink' => '', 'categories' => get_the_category());
            // if a shortlink exists, let's use that.
            if (function_exists('wp_get_shortlink')) {
                $postObject['shortlink'] = wp_get_shortlink($post->ID, 'post');
            } else {
                $postObject['shortlink'] = $postObject['permalink'];
            }
            $object['posts'][] = $postObject;
        }
    }
    $api->jsonize($object);
}