【发布时间】:2016-03-08 10:38:36
【问题描述】:
我正在使用 json 编码获取我的类别,因此我的格式化数据是 json 格式,并且该数据正在移动应用程序中使用,目前除了类别之外,一切都运行良好。加载类别需要 8 到 10 秒。 这是我为完成此任务而编写的数据
public function category() {
global $post; global $cate_id;
if(isset($_GET['category_id'])){
$cat_id = $_GET['category_id'];
//$cat = get_category($cat_id);
//$cat_name = $cat->slug;
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'category' => $cat_id
//'category_name' => 'cancer-care'
);
$posts = get_posts($args);
}
//print_r($posts);
if(!empty($posts)){
foreach ($posts as $post) : setup_postdata( $post );
$post_title = $post->post_title;
$post_content = $post->post_excerpt;
$post_fullcontent = apply_filters ("the_content", $post->post_content);//$post->post_content;
$post_link = get_the_permalink($post->ID);
$post_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ,'thumbnail_size');
$post_image_thumb = get_bloginfo('template_url').'/thumbs/timthumb.php?src='.$post_image.'&w=438&h=220&zc=1&a=c&q=100';
$category_name = get_cat_name(get_post_meta($post->ID, 'home_page_category', true));
$category_id = get_post_meta($post->ID, 'home_page_category', true);
$category_color = categoryCustomFields_GetCategoryCustomField($category_id, 'Color Code');
$category_url = get_category_link(get_post_meta($post->ID, 'home_page_category', true));
//earlier done: echo $totalcount = $this->social_shares($post_link);
$result[] = array(
'post_id'=>$post->ID,
'post_title' => $post_title,
'post_short_content' => $post_content,
'post_full_content' => $post_fullcontent,
'post_link' => $post_link,
'post_image' => $post_image,
'post_image_thumb' => $post_image_thumb,
'category_name' => $category_name,
'category_color_code' => $category_color[0]->field_value ? $category_color[0]->field_value : '#83ab44',
'category_id' => $category_id,
'category_url' => $category_url,
'total_social_share' =>$this->social_shares($post_link),
'post_by' =>get_the_author(),
'post_date' =>date('Y-m-d', strtotime($post->post_date)),
'post_time' =>date('H:i:s', strtotime($post->post_date))
);
endforeach;
$message = array(
"success" => "true",
"error" => "null",
"post_data" => $result
);
echo json_encode(array('response' => $message));
}else{
$message = array(
"success" => "false",
"error" => "Record not available",
"post_data" => "Record not available"
);
echo json_encode(array('response' => $message));
}
}
只要使用我的所有其他数据,例如 home_psots 函数来显示主页帖子,或 feature_post 函数来显示特色帖子等,就会突然显示结果......而不是类别......因为我得到了像这样的类别这个
category?category_id=4 任何想法,这样我就可以比这个更快地获取我的数据...... ???我已经尝试了很多东西,甚至使用.htaccess 文件来重定向到我编写的其他函数但徒劳无功...:(
【问题讨论】:
-
问题可能是这个
categoryCustomFields_GetCategoryCustomField函数...你能提供它的代码吗? -
感谢您的回答,但事实并非如此.. 即使我评论了整行,它仍然会向我抛出 10 秒:(
-
注释
$categories...开头的4行解决了吗? -
嗯...我现在能给你的唯一建议是尝试找到有问题的部分。评论 foreach 是否足够快?也许
get_posts是慢的部分... -
get_posts 不是罪魁祸首......罪魁祸首部分看起来像 url 中的参数......因为如果我只使用类别函数并添加静态类别 slug 或 id,那么它会突然加载。 ..只要我像从 url 获取 category_id 一样动态,它就会减慢到 10 秒...希望你知道
标签: php wordpress categories