通过使用Transients API缓存自定义查询来加速您的WordPress

哦,男孩,标题听起来很可怕不是吗。你没有什么可担心的,因为我们会把它全部打破。您的主题是否运行自定义WordPress查询以在侧边栏或其他任何位置显示随机帖子,热门帖子,最近帖子等?如果是,那么您应该考虑使用WordPress瞬态API来缓存这些查询,以减少资源消耗并帮助加载时间。在本文中,我们将向您展示如何通过使用Transients API缓存自定义查询来加速WordPress网站。

注意:您需要了解WordPress主题如何工作(循环等),以便您关注这篇文章。

所以这整个缓存和瞬态术语都在我脑海中。好吧不要担心让我们解释它的作用。基本上,如果您正在运行像List25这样的站点,并且在侧栏中有一个显示6个随机帖子的循环,那么瞬态API可以提供帮助。每次用户刷新页面时,您拥有的自定义WP查询将进入您的数据库并随机提取6个帖子。如果你是一个相对较新的网站,那应该不会那么糟糕。但是,如果您的网站上有很多人,那么它可能会导致SQL服务器崩溃,您将看到“建立数据库连接错误”屏幕。通过添加一些额外的代码行,您可以使用Transients API轻松地将该查询的结果(缓存它)存储一段时间。

我们用于提取随机帖子的循环代码示例:

  <?php $random_query = new WP_Query("orderby=rand&posts_per_page=6");  while ($random_query->have_posts()) : $random_query->the_post();  ?>  <div >  <div ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>  <div >  <div ><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>  </div>  </div>  <?php endwhile; ?>  

关于我们在侧边栏中的随机帖子查询最酷的部分是每次都显示新内容。因此,通过缓存查询12小时,我们将有相同的6个帖子显示12小时对吗?好吧,由于我们的朋友Konstantin Kovshenin(@kovshenin)的建议,我们找到了一个工作。他建议使用get_posts而不是使用WP_Query,而是使用20个帖子。使用瞬态API缓存该查询的结果,然后使用array_rand()函数随机显示原始20中的6个帖子。这样我们就可以在网站上模拟随机效果了。

我们做的第一件事是设置瞬态。我们从WordPress Codex页面获得了代码。

  // Get any existing copy of our transient data  if ( false === ( $special_query_results = get_transient( "special_query_results" ) ) ) {      // It wasn"t there, so regenerate the data and save the transient  $randargs = array("orderby" => "rand", "numberposts" => 20);  $special_query_results = get_posts($randargs);      set_transient( "special_query_results", $special_query_results, 60*60*12 );  }  

请注意,60 * 60 * 12是您可以控制缓存长度的区域。随意将其更改为您喜欢的任何内容。现在,如果我们使用foreach循环显示$ special_query_results,我们将显示所有20个帖子。所以我们需要利用array_rand()函数来随机抽取6个项目。我们添加了这样的代码:

  $randomposts = get_transient( "special_query_results" );  $randkey = array_rand( $randomposts, 6 );  

现在,这将从我们的瞬态数据中随机抽出6个帖子ID。但是,它不会为每个帖子提取值。所以我们不得不添加这些代码:

  $sixposts[0] = $randomposts[$randkey[0]];  $sixposts[1] = $randomposts[$randkey[1]];  $sixposts[2] = $randomposts[$randkey[2]];  $sixposts[3] = $randomposts[$randkey[3]];  $sixposts[4] = $randomposts[$randkey[4]];  $sixposts[5] = $randomposts[$randkey[5]];  

基本上我们为$ sixposts创建了一个数组,我们在其中为每个项目分配一个值。不确定这是否是最佳方式,但它确实有效。如果您有任何更好的建议,请随时在评论中发布。

完成后,我们现在准备显示循环。简单地把代码放在这样:

  global $post; //required for it to work  foreach( $sixposts as $post ) :  setup_postdata($post);    //All the items go here.    endforeach;  

setup_postdata允许你使用这个foreach循环中的所有循环标记,例如the_permalink等。

为了方便每个人,这里是我们的最终代码:

  <?php  // Get any existing copy of our transient data  if ( false === ( $special_query_results = get_transient( "special_query_results" ) ) ) {      // It wasn"t there, so regenerate the data and save the transient  $randargs = array("orderby" => "rand", "numberposts" => 20);  $special_query_results = get_posts($randargs);      set_transient( "special_query_results", $special_query_results, 60*60*12 );  }    // Use the data like you would have normally...  $randomposts = get_transient( "special_query_results" );  $randkey = array_rand( $randomposts, 6 );  $sixposts[0] = $randomposts[$randkey[0]];  $sixposts[1] = $randomposts[$randkey[1]];  $sixposts[2] = $randomposts[$randkey[2]];  $sixposts[3] = $randomposts[$randkey[3]];  $sixposts[4] = $randomposts[$randkey[4]];  $sixposts[5] = $randomposts[$randkey[5]];    global $post;  foreach( $sixposts as $post ) :  setup_postdata($post); ?>    <div >  <div ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>  <div >  <div ><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>  </div>  </div>    <?php endforeach; ?>  

哦,男孩,标题听起来很可怕不是吗。你没有什么可担心的,因为我们会把它全部打破。您的主题是否运行自定义WordPress查询以在侧边栏或其他任何位置显示随机帖子,热门帖子,最近帖子等?如果是,那么您应该考虑使用WordPress瞬态API来缓存这些查询,以减少资源消耗并帮助加载时间。在本文中,我们将向您展示如何通过使用Transients API缓存自定义查询来加速WordPress网站。

注意:您需要了解WordPress主题如何工作(循环等),以便您关注这篇文章。

所以这整个缓存和瞬态术语都在我脑海中。好吧不要担心让我们解释它的作用。基本上,如果您正在运行像List25这样的站点,并且在侧栏中有一个显示6个随机帖子的循环,那么瞬态API可以提供帮助。每次用户刷新页面时,您拥有的自定义WP查询将进入您的数据库并随机提取6个帖子。如果你是一个相对较新的网站,那应该不会那么糟糕。但是,如果您的网站上有很多人,那么它可能会导致SQL服务器崩溃,您将看到“建立数据库连接错误”屏幕。通过添加一些额外的代码行,您可以使用Transients API轻松地将该查询的结果(缓存它)存储一段时间。

我们用于提取随机帖子的循环代码示例:

  <?php $random_query = new WP_Query("orderby=rand&posts_per_page=6");  while ($random_query->have_posts()) : $random_query->the_post();  ?>  <div >  <div ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>  <div >  <div ><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>  </div>  </div>  <?php endwhile; ?>  

关于我们在侧边栏中的随机帖子查询最酷的部分是每次都显示新内容。因此,通过缓存查询12小时,我们将有相同的6个帖子显示12小时对吗?好吧,由于我们的朋友Konstantin Kovshenin(@kovshenin)的建议,我们找到了一个工作。他建议使用get_posts而不是使用WP_Query,而是使用20个帖子。使用瞬态API缓存该查询的结果,然后使用array_rand()函数随机显示原始20中的6个帖子。这样我们就可以在网站上模拟随机效果了。

我们做的第一件事是设置瞬态。我们从WordPress Codex页面获得了代码。

  // Get any existing copy of our transient data  if ( false === ( $special_query_results = get_transient( "special_query_results" ) ) ) {      // It wasn"t there, so regenerate the data and save the transient  $randargs = array("orderby" => "rand", "numberposts" => 20);  $special_query_results = get_posts($randargs);      set_transient( "special_query_results", $special_query_results, 60*60*12 );  }  

请注意,60 * 60 * 12是您可以控制缓存长度的区域。随意将其更改为您喜欢的任何内容。现在,如果我们使用foreach循环显示$ special_query_results,我们将显示所有20个帖子。所以我们需要利用array_rand()函数来随机抽取6个项目。我们添加了这样的代码:

  $randomposts = get_transient( "special_query_results" );  $randkey = array_rand( $randomposts, 6 );  

现在,这将从我们的瞬态数据中随机抽出6个帖子ID。但是,它不会为每个帖子提取值。所以我们不得不添加这些代码:

  $sixposts[0] = $randomposts[$randkey[0]];  $sixposts[1] = $randomposts[$randkey[1]];  $sixposts[2] = $randomposts[$randkey[2]];  $sixposts[3] = $randomposts[$randkey[3]];  $sixposts[4] = $randomposts[$randkey[4]];  $sixposts[5] = $randomposts[$randkey[5]];  

基本上我们为$ sixposts创建了一个数组,我们在其中为每个项目分配一个值。不确定这是否是最佳方式,但它确实有效。如果您有任何更好的建议,请随时在评论中发布。

完成后,我们现在准备显示循环。简单地把代码放在这样:

  global $post; //required for it to work  foreach( $sixposts as $post ) :  setup_postdata($post);    //All the items go here.    endforeach;  

setup_postdata允许你使用这个foreach循环中的所有循环标记,例如the_permalink等。

为了方便每个人,这里是我们的最终代码:

  <?php  // Get any existing copy of our transient data  if ( false === ( $special_query_results = get_transient( "special_query_results" ) ) ) {      // It wasn"t there, so regenerate the data and save the transient  $randargs = array("orderby" => "rand", "numberposts" => 20);  $special_query_results = get_posts($randargs);      set_transient( "special_query_results", $special_query_results, 60*60*12 );  }    // Use the data like you would have normally...  $randomposts = get_transient( "special_query_results" );  $randkey = array_rand( $randomposts, 6 );  $sixposts[0] = $randomposts[$randkey[0]];  $sixposts[1] = $randomposts[$randkey[1]];  $sixposts[2] = $randomposts[$randkey[2]];  $sixposts[3] = $randomposts[$randkey[3]];  $sixposts[4] = $randomposts[$randkey[4]];  $sixposts[5] = $randomposts[$randkey[5]];    global $post;  foreach( $sixposts as $post ) :  setup_postdata($post); ?>    <div >  <div ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>  <div >  <div ><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>  </div>  </div>    <?php endforeach; ?>  

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 13:04:05
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: