Php WordPress类别在页面加载方面花费了大量时间
我用json编码得到我的分类,所以我的格式化数据是json格式的,这些数据正在移动应用程序中使用,目前除了分类之外,一切都很好。类别加载需要8到10秒。 这是我为完成这项任务而编写的数据Php WordPress类别在页面加载方面花费了大量时间,php,wordpress,categories,Php,Wordpress,Categories,我用json编码得到我的分类,所以我的格式化数据是json格式的,这些数据正在移动应用程序中使用,目前除了分类之外,一切都很好。类别加载需要8到10秒。 这是我为完成这项任务而编写的数据 public function category() { global $post; global $cate_id; if(isset($_GET['category_id'])){ $cat_id = $_GET['category_id']; //$cat
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\u psots
显示主页帖子的功能,或feature\u post
显示特色帖子的功能等,就会突然显示结果。。。而不是类别。。。因为我得到了像这样的分类
category?category_id=4有什么想法可以让我更快地获取数据吗???我已经尝试了很多东西,甚至使用.htaccess
文件重定向到我编写的其他函数,但没有成功…:(引用自:
注意:如果不设置offset参数,posts\u per\u page参数将不起作用
也许wordpress正在查询该类别中的所有帖子,这正在放缓
请尝试以下代码:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_id,
);
请记住,“category”
需要category ID,即整数
希望有帮助!这就是页面加载时间的全部区别。正在缓存加载的其他没有URL参数的页面,并且在进一步加载时花费的时间更少。而W3 Total Cache插件在使用磁盘增强的页面缓存方法时不缓存基于查询的URI。这就是为什么不缓存基于查询的页面的原因d每次你加载页面时,它都会被再次抓取,这需要时间。希望你明白这一点,为了让它缓存,你可以应用其他技术,比如你可以创建类别页面,并将该页面缓存在你需要的类别可以显示的地方等。为了进一步阅读,这里是插件作者自己的另一个回复
希望到目前为止这一切都有意义。问题可能是这个
categoryCustomFields\u GetCategoryCustomField
函数……你能提供它的代码吗?谢谢你的回答,但事实并非如此。即使我评论了整行,它仍然会向我扔下10秒钟的时间:(注释以$categories…
开头的4行…解决它?嗯…我现在能给你的唯一建议是尝试找到问题的部分。注释foreach使它足够快?也许get_posts
是慢的部分…get_posts不是罪魁祸首…罪魁祸首部分看起来像url中的参数…因为如果我o仅使用category函数并添加一个静态类别slug或id,然后它会突然加载…一旦我进入动态状态(如从url获取category_id),它会减慢到10秒…希望您知道是的,我知道,但我在所有函数中都做了相同的操作,并且所有函数都提供了很好的速度,虽然我现在已经更新了添加偏移量的代码,但仍然需要时间这里有一个问题…你能提供使代码运行更快的编辑吗?这几乎是相同的数据…唯一的区别是,在上面的情况下,我从URL获取id号,而在home_posts函数中,我使用的代码几乎与上面所示的相同。你在其他函数中是否使用了比category函数加载更快的缓存方法我们正在使用W3 Total Cache插件来缓存我们的网站,除了这个分类功能,一切都进行得非常顺利。明白你的意思了,让我试试其他任何一种方式,比如slug分类,如果可以的话。哇,现在效果很好,我为我的癌症护理分类创建了category-cancer-care.php页面,它显示并缓存了非常好的页面和加载时间只需2秒…这样我就可以为类别创建其他模板…非常感谢你,你是我的英雄,因为你救了我的工作。。。