对WordPress主loop主循环查询进行缓存(简单/复杂两种)

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

[cc] 100,
‘post_type’ => ‘tweets’,
‘post_status’ => ‘publish’ ) ;

$loop = new WP_Query($query);

// transient set to last for 1 hour
set_transient(‘loop’, $loop, 60*60);
}
// do normal loop stuff
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();

// show content or whatever you like
the_content();

endwhile;endif;

?>[/cc]


100,
'post_status' => 'publish' ) ;

// run the query
$loop = new WP_Query($query);

// start the output buffer to save contents of loop
ob_start();

// do normal loop stuff
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();

// show content or whatever you like
?>


缓存短代码示例
[cc]// shortcode usage
// [my_gallery id=”1″ height=”300″ width=”200″]

function shortcode_gallery( $atts ){
// get the values from inside the shortcode and make them variables
// the id, height and width have the default settings inside the array if not set
// in the shortcode.
extract( shortcode_atts( array(
‘id’ => ‘1’,
‘height’ => ‘100’,
‘width’ => ’60’,
), $atts ) );

$gallery = get_transient(‘gallery’ . $id . $height . $width );
if($gallery === false){
//
// Do the code that creates your gallery and return the output to a variable called Gallery
$gallery = get_my_gallery($id, $height, $width);

// save the count for 30 days
set_transient(‘gallery’ . $id . $height . $width, $gallery, 60*60*24*30 );
}
// return the response. **do not echo**
return $gallery;
}
add_shortcode( ‘my_gallery’, ‘shortcode_gallery’ );[/cc]

参数详见https://cloud.tencent.com/developer/article/1559253

'featured',
'posts_per_page' => 5
)
);

// Put the results in a transient. Expire after 12 hours.
set_transient( 'foo_featured_posts', $featured, 12 * HOUR_IN_SECONDS );
}
?>

// Run the loop as normal

have_posts() ) : ?>

have_posts() ) : $featured->the_post(); ?>
// featured posts found, do stuff


// no featured posts found

Leave a Comment