我想遍历一个数组并获取每个帖子的ID并使用它来查询更多信息并将其注入数组。
我该怎么做?目前有两个单独的脚本可以执行此操作,但由于管理要求,它们必须是一个。
这是第一个返回所有帖子的脚本
$offset = $_GET['offset'];
$numberposts = $_GET['numberposts'];
$category_id = $_GET['category_id'];
global $wpdb;
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category' => $category_id,
'orderby' => 'post_date',
'exclude' => -21,
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => '_thumbnail_id',
'posts_per_page' => 2 );
$posts['success'] = get_posts($args);
if (!$posts['success']) {
$msg['error']['id'] = "no_posts";
$msg['error']['description'] = "This category has no posts.";
echo json_encode($msg);
} else {
echo strip_tags(json_encode($posts), '<p></p><br><br />');
}
?>
返回的示例网址 http://monkeypantsstudios.com/lab/newkoa/requestPosts.php
根据给定的post_id返回图片网址的脚本。
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/lab/newkoa/wp-config.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/lab/newkoa/wp-includes/media.php');
$image['success'] = wp_get_attachment_image_src(get_post_thumbnail_id($_GET['post_id']), array(100, 100) );
if (!$image['success'])
{
$error['error']['ID'] = 'no_featured_image';
$error['error']['description'] = 'This post does not have a featured image.';
echo json_encode($error);
}
else
{
echo json_encode($image);
}
?>
这是示例网址: http://monkeypantsstudios.com/lab/newkoa/requestFeaturedImage.php
我已经足够构建API到这一点,但不足以合并这两个脚本,因为它们需要更多的技巧。
帮助?