我很确定我一直在这里读到new WP_Query()
建议超过query_posts
. 此外,您还可以使用Transient API 提高其他查询的性能。将其放置在希望列表显示的任何模板中:
// Get any existing copy of our transient data
if ( false === ( $my_special_tag = get_transient( \'my_special_tag\' ) ) ) {
// It wasn\'t there, so regenerate the data and save the transient
// params for our query
$args = array(
\'tag\' => \'foo\'
\'posts_per_page\' => 5,
);
// The Query
$my_special_tag = new WP_Query( $args );
// store the transient
set_transient( \'my_special_tag\', $my_special_tag, 12 * HOUR_IN_SECONDS );
}
// Use the data like you would have normally...
// The Loop
if ( $my_special_tag ) :
echo \'<ul class="my-special-tag">\';
while ( $my_special_tag->have_posts() ) :
$my_special_tag->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
endwhile;
echo \'</ul>\';
else :
echo \'No posts found.\';
endif;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren\'t stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
在你的功能中。php更新时,您需要清除瞬态:
// Create a function to delete our transient when a post is saved or term is edited
function delete_my_special_tag_transient( $post_id ) {
delete_transient( \'my_special_tag\' );
}
add_action( \'save_post\', \'delete_my_special_tag_transient\' );
add_action( \'edit_term\', \'delete_my_special_tag_transient\' );