Skip to content

Instantly share code, notes, and snippets.

@juarezpaf
Created September 24, 2009 00:57
Show Gist options
  • Save juarezpaf/192410 to your computer and use it in GitHub Desktop.
Save juarezpaf/192410 to your computer and use it in GitHub Desktop.
/*
WordPress shortcode: Display the loop
From http://www.wprecipes.com/wordpress-shortcode-display-the-loop/
*/
<?php
function myLoop($atts, $content = null) {
extract(shortcode_atts(array(
"pagination" => 'true',
"query" => '',
"category" => '',
), $atts));
global $wp_query,$paged,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
if($pagination == 'true'){
$query .= '&paged='.$paged;
}
if(!empty($category)){
$query .= '&category_name='.$category;
}
if(!empty($query)){
$query .= $query;
}
$wp_query->query($query);
ob_start();
?>
<h2><?php echo $category; ?></h2>
<ul class="loop">
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php echo $thumbnail_image; the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php if(pagination == 'true'){ ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php } ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("loop", "myLoop");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment