帖子太多会导致分页时加载缓慢

时间:2012-12-14 作者:Ruriko

我有大约10k篇帖子,我制作了一个页面,列出了所有帖子,但每54篇帖子都会分页。它在6.730秒内提供131个查询,加载速度非常慢。有没有办法让它加载得更快?

这是我的功能页面

<?php
/* Custom code goes below this line. */

add_theme_support( \'post-formats\', array( \'aside\', \'gallery\' ) );
add_filter( \'use_default_gallery_style\', \'__return_false\' );

/* Add the Javascript */
$path = get_stylesheet_directory_uri() .\'/js/\';
wp_enqueue_script(\'post-navigation\', $path.\'jquery.navigate.js\', array(\'jquery\'));

function catch_that_image() {
global $post, $posts;
$first_img = \'\';
ob_start();
ob_end_clean();


$transformed_content = apply_filters(\'the_content\',$post->post_content);


$output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $transformed_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}

function pagination($pages = \'\', $range = 4)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == \'\')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class=\\"pagination\\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href=\\"".get_pagenum_link($paged + 1)."\\">Next &rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a>";
         echo "</div>\\n";
     }
}

// This function should go in functions.php
function mam_posts_where ($where) {
   global $mam_global_where;
   if ($mam_global_where) $where .= " $mam_global_where";
   return $where;
}
add_filter(\'posts_where\',\'mam_posts_where\');

/* Custom code goes above this line. */
?>
这是我的页面模板

<?php
/*
Template Name: List
*/
?>

<?php get_header(); ?>

        <div id="container">
            <div id="content2" role="main">

<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array( \'post_type\' => \'post\', \'posts_per_page\' => 54, \'paged\' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<div class="seriesbox">
<a class="size-thumbnail" href="<?php the_permalink(); ?>"><img src="<?php echo catch_that_image() ?>"/></a>
<div class="seriestitle"><h2><?php the_title() ?></h2></div>
</div>
<?php endwhile; ?>

<!-- then the pagination links -->
<?php if (function_exists("pagination")) {
    pagination($additional_loop->max_num_pages);
} ?>

            </div><!-- #content -->
        </div><!-- #container -->

        <?php get_footer(); ?>

1 个回复
SO网友:Mark Kaplun

是的,使用缓存。但是,即使使用缓存,您也应该重新考虑您实际在做什么,因为每次发布新帖子时,页面/2的缓存都应该更改,这可能会使缓存变得毫无意义,这取决于该页面的流量。你可能应该重新思考你在那里想要实现什么,为什么是54而不是更少?一个页面上有54个图像太多了,页面速度会很慢,这不仅是因为HTML生成,还因为获取图像需要时间。

结束

相关推荐

wp_list_categories pagination

我想知道我是否可以为wp\\U list\\U类别分页?我尝试了paginate\\u链接,但运气不好。还是有好的解决方法?非常感谢。<?php $orderby = \'name\'; $show_count = 0; $pad_counts = 0; $hierarchical = 1; $taxonomy = \'categories\'; $title = \'\'; $exclude = \