DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

By default, the loop is the easiest way to display WordPress posts. Though, we can create a shortcode that will make post displaying ven simpler. Perfect for sites who are using WordPress as a CMS!

Simply paste this code in your functions.php file. Or if you want, you can use it in a plugin.

function myLoop($atts, $content = null) {
		"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;
		$query .= '&category_name='.$category;
		$query .= $query;
	<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; ?>
	<?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>
	<?php } ?>
	<?php $wp_query = null; $wp_query = $temp;
	$content = ob_get_contents();
	return $content;
add_shortcode("loop", "myLoop");

Once your functions.php file is saved, you can display a loop :

[loop category="news" query="" pagination="false"]

Note that this code have been created to been used in pages. It have some oddity when used in a post.

Thanks to John Turner for this great piece of code!


  1. Awesome,

    Could you please tell me how to add limit to this category specified loop.

    I want 3 items to display at front.


  2. Can one use the regular loop inside a shortcode? eg: if have posts : while have posts : the_post , etc…?

  3. Awesome! Just a quick tip. I’m using this on a WP-Framework child theme. In order to get the regular loop as your output simply throw in:

    get_template_part( ‘loop’, ‘archive’ );

  4. Lucas!!!! You have no idea how much your post helped me out!!! After 3 long days of no sleep and pulling my hair out trying to accomplish a short code loop using the main loop, your post saved my life!!!!!!!!!!!!!!!!!!!

  5. is it possible to do the same for custom post types? Ive been searching unsuccesfully for a way to display paginated results of a custom post type

  6. Hello, How can I add more attributes like, “order”, “posts_per_page”, etc. Thanks!

  7. Can this be modified so that I can make a pause in the loop?

    Start loop news
    Enter f.ex. a text widget
    continue loop news
    end with pagination of the loop news

  8. Nice code! Thank you!!

    To limit posts you can just add the following. I know it is not the correct fix for the shortcode but works if it is in the string…

    $query .= ‘&category_name=’.$category;
    $query .= ‘&posts_per_page=’.’2′;

Leave a Comment

Your email address will not be published. Required fields are marked *