Skip to content

Instantly share code, notes, and snippets.

@johndugan
Created July 12, 2012 03:27
Show Gist options
  • Save johndugan/3095492 to your computer and use it in GitHub Desktop.
Save johndugan/3095492 to your computer and use it in GitHub Desktop.
WordPress: loop shortcode for pages
// --------------------------------------------------
// ---------- Create custom loop shortcode ----------
// --------------------------------------------------
// usage: [loop cat="17" posts_per_page="6" ]
// --------------------------------------------------
<?php
add_shortcode('loop', 'shortcode_query');
function shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values. more query params at http://codex.wordpress.org/Class_Reference/WP_Query
'posts_per_page' => 4,
'caller_get_posts' => 1,
'post__not_in' => get_option('sticky_posts'),
), $atts));
global $post;
$posts = new WP_Query($atts);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
?>
<article <?php post_class() ?>>
<footer class="entry-meta">
<time datetime="<?php echo date(DATE_W3C); ?>" pubdate class="updated"><?php the_time('F jS, Y') ?></time>
<small>in <?php the_category(', ') ?></small>
</footer>
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="" >
<?php echo clean_img_width_height(get_the_post_thumbnail(get_the_ID())); ?>
</a>
<?php endif; ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
</article>
<?php
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($output);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment