Created
January 21, 2013 18:02
-
-
Save WebEndevSnippets/4587952 to your computer and use it in GitHub Desktop.
WordPress: Custom Loop with WP_Query and Transient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shireman_books_header_loop( $args ) { | |
global $post; | |
$defaults = array( | |
'orderby' => 'rand', | |
'post_type' => 'we_published-book', | |
'posts_per_page' => 9, | |
'post_status' => 'publish', | |
'no_found_rows' => true, // counts posts, remove if pagination required | |
'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...) | |
'update_post_meta_cache' => false, // grabs post meta, remove if post meta required | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
if ( false === ( $bookthumbs = get_transient( 'we_books_header_' . $post->ID ) ) ) { | |
$bookthumbs = new WP_Query( $args ); | |
// Set transient for 1 day | |
set_transient( 'we_books_header_' . $post->ID, $bookthumbs, 60*60*24 ); | |
} | |
if ( $bookthumbs->have_posts() ) { | |
echo '<ul>'; | |
while ( $bookthumbs->have_posts() ) : $bookthumbs->the_post(); | |
if( has_post_thumbnail() ) { | |
printf( '<li><a href="%s" title="%s"><span class="roll" ></span>%s</a></li>', get_permalink(), get_the_title(), genesis_get_image( array( 'format' => 'html', 'size' => 'book-small', ) ) ); | |
} | |
endwhile; | |
echo '</ul>'; | |
} | |
wp_reset_query(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment