【问题标题】:WordPress categories are taking huge time in page loadWordPress 类别在页面加载中需要大量时间
【发布时间】: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


【解决方案1】:

这就是页面加载时间的全部差异。正在加载没有 URL 参数的其他页面正在被缓存,并且在进一步加载中花费的时间更少。当使用磁盘增强页面缓存方法时,W3 Total Cache Plugin 不会缓存基于查询的 URIs。这就是为什么您的基于查询的页面没有被缓存并且每次加载页面时都会再次获取它并且需要时间。希望您明白这一点,要使其缓存,您可以应用其他技术,例如创建类别页面并缓存可以显示所需类别的页面等。进一步阅读这里是插件作者本人的另一个回复https://wordpress.org/support/topic/plugin-w3-total-cache-cache-query-urls-disabled 希望现在说得通。

【讨论】:

  • 明白你的意思,让我尝试任何其他方式,如通过 slug 进行分类,如果可行的话
  • 哇,现在效果很好,我为我的癌症护理类别创建了 category-cancer-care.php 页面,它显示和缓存页面非常好,加载时间缩短到 2 秒....通过这种方式,我将为类别创建其他模板......非常感谢你是我的英雄,因为你拯救了我的工作......
【解决方案2】:

引用自docs

注意:posts_per_page 参数在不设置 offset 参数的情况下不起作用

也许 wordpress 正在查询该类别中的所有帖子并且速度变慢了。

试试这个代码:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'offset' => 0,
    'category' => $cat_id,
);

请记住,'category'需要类别 ID,即整数。

希望对你有帮助!

【讨论】:

  • 是的,我知道,但我在所有功能中都在做同样的事情,而且速度都很好,虽然我现在已经更新了添加偏移量的代码,但问题仍然存在......
  • 你能提供使代码运行得更快的编辑吗?
  • 这几乎是相同的数据......唯一的区别是我在上述情况下从 URL 获取 id 号,而在 home_posts 函数中我使用的代码几乎与上面显示的代码相同
  • 您是否在其他函数中使用了任何比类别函数加载速度更快的缓存方法
  • 是的,我们正在使用 W3 Total Cache 插件来缓存我们的网站,除了这个分类功能之外一切都很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
相关资源
最近更新 更多