Skip to content

Instantly share code, notes, and snippets.

@trovster
Created July 13, 2012 11:35
Show Gist options
  • Select an option

  • Save trovster/3104443 to your computer and use it in GitHub Desktop.

Select an option

Save trovster/3104443 to your computer and use it in GitHub Desktop.
Latest Posts
<?php
/**
* template_pre_loop
* @desc Used before a custom loop
* Parameter is the WP_Query options for the new loop
* Saves the original query and post data
* Returns the new query, along with the original wp_query and post
* @param array $options
* @return array
*/
function template_pre_loop($options) {
global $wp_query, $post;
$original_post = null;
$original_wp_query = null;
if(!empty($post)) {
$original_post = clone $post;
}
if(!empty($wp_query)) {
$original_wp_query = clone $wp_query;
}
$wp_query = new WP_Query($options);
return compact('wp_query', 'original_wp_query', 'original_post');
}
/**
* template_post_loop
* @desc Used after a custom loop
* Parameter is the original array saved from the pre loop function
* Resets the query and post data
* Returns the original wp_query and post data
* @param array $original
* @return array
*/
function template_post_loop($original) {
global $wp_query, $post;
extract($original);
if(!empty($original_wp_query)) {
$wp_query = clone $original_wp_query;
}
if(!empty($original_post)) {
$post = clone $original_post;
}
wp_reset_query();
return compact('wp_query', 'post');
}
<?php
$setup = array(
'post_type' => 'post',
'title_li' => '',
'echo' => 0,
'depth' => 1,
'posts_per_page' => 3,
/*
// limit to posts with a thumbnail
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => '',
)
)
*/
);
?>
<?php $original = template_pre_loop($setup); ?>
<?php get_template_part('loop', 'post-latest'); ?>
<?php $original = template_post_loop($original); extract($original); wp_reset_query(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment