WordPress 类别在页面加载上花费了大量时间
WordPress categories are taking huge time in page load
我正在使用 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
文件来重定向到我写的其他功能但是徒劳...:(
转自docs:
Note: The posts_per_page parameter does NOT work without setting the offset parameter
也许 wordpress 正在查询该类别中的所有帖子,而且速度变慢了。
试试这个代码:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_id,
);
请记住,'category'
需要类别 ID,即整数。
希望对您有所帮助!
这就是页面加载时间的全部差异。其他没有 URL 参数加载的页面被缓存,并且在进一步加载时花费更少的时间。当使用磁盘增强页面缓存方法时,W3 Total Cache Plugin 不会缓存基于查询的 URis。这就是为什么您的基于查询的页面没有被缓存,并且每次您加载页面时它都会被再次获取并且需要时间。希望你明白这一点,为了让它缓存你可以应用其他技术,比如你可以创建类别页面并缓存可以显示所需类别的页面等。为了进一步阅读,这里是插件作者本人的另一个回复 https://wordpress.org/support/topic/plugin-w3-total-cache-cache-query-urls-disabled
希望现在说得通了。
我正在使用 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
文件来重定向到我写的其他功能但是徒劳...:(
转自docs:
Note: The posts_per_page parameter does NOT work without setting the offset parameter
也许 wordpress 正在查询该类别中的所有帖子,而且速度变慢了。
试试这个代码:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_id,
);
请记住,'category'
需要类别 ID,即整数。
希望对您有所帮助!
这就是页面加载时间的全部差异。其他没有 URL 参数加载的页面被缓存,并且在进一步加载时花费更少的时间。当使用磁盘增强页面缓存方法时,W3 Total Cache Plugin 不会缓存基于查询的 URis。这就是为什么您的基于查询的页面没有被缓存,并且每次您加载页面时它都会被再次获取并且需要时间。希望你明白这一点,为了让它缓存你可以应用其他技术,比如你可以创建类别页面并缓存可以显示所需类别的页面等。为了进一步阅读,这里是插件作者本人的另一个回复 https://wordpress.org/support/topic/plugin-w3-total-cache-cache-query-urls-disabled 希望现在说得通了。