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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$loop = get_transient( 'loop' );
if ( false === $loop ) {

       // Show the last 100 tweets from the custom post type tweets.
       $query = array('post_per_page' => 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;

?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
$loop_output = get_transient( 'loopOutput' );
if ( false === $loop_output ) {

       // Show the last 100 published posts.
       $query = array('post_per_page' => 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
                ?><h1><?php the_title() ?></h1><?php
                the_content();

            endwhile;endif;

            // save the output buffer contents in a variable
            $loop_output = ob_get_contents();

       // clean the buffer as we will be using the variable from now on
       ob_end_clean();

       // transient set to last for 1 hour
       set_transient('loopOutput', $loop_output, 60*60);
}

// output the new created loop if loop content does not exist.
echo $loop_output;

?>

缓存短代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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' );

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
// Check for transient. If none, then execute WP_Query
if ( false === ( $featured = get_transient( 'foo_featured_posts' ) ) ) {
    $featured = new WP_Query(
        array(
            'category' => '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
 
<?php if ( $featured->have_posts() ) : ?>
 
    <?php while ( $featured->have_posts() ) : $featured->the_post(); ?>
        // featured posts found, do stuff
    <?php endwhile; ?>
 
<?php else: ?>
    // no featured posts found
<?php endif; ?>
 
<?php wp_reset_postdata(); ?>