Post Pic

WordPress shortcode: Display the loop

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) {
	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");

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!

22 Responses

Sep 23 2009 11:48

nice…….

Sep 23 2009 15:33

Hey there… I think it’s very easy to implement and more to adapt.
One little question: Why use ob_start(); function there? I didn’t understand very well.
“Note that this code have been created to been used in pages…” I can’t use it in category? The shortcode example is using one category name.

Sep 23 2009 15:49

Hi Juarez, I use ob_start so I can capture the output from the template tags. Not all template tags allow you to pass in an echo true|false param. If you don’t capture the buffer it will display those tag instead of returning everything as a whole.

Sep 24 2009 02:55

@John Thanks for explanation. I’ll going deep inside this ob_start to know more about it. In one project I had some issues with multiple loops at same page and I thinks with this shortcode I can eliminate this headache. Keep moving!

Sep 24 2009 11:57

Excellent recipe, I think I’ll rate this one 5/5

Sep 25 2009 23:59

This is great! Thanks!

Sep 26 2009 19:48

Hey @John it doesn’t work to me. I did put this function inside my function.php and the shortcode I inserted in my category.php
What am I doing wrong? :(

Sep 26 2009 22:25

@Juarez, this short code is ment to be used on a page. so for example, create a new page in side of WordPress, then in the editor paste the shortcode. If you want to use the code inside of a template you need to call do_shortcode(). Here’s more info. http://codex.wordpress.org/Shortcode_API

Sep 28 2009 08:44

Hey @John thanks one more time for this advice, I’ll try it tomorrow. I was thinking and I decided to convert your shortcode to a function inside of function.php and use it to handle multiple loops. I have some trouble with Multiple loops in WordPress because I use many customized areas and the “$temp = $wp_query;” hack didn’t work very well in my last try.

Jan 02 2010 06:08

In your shortcode…
[loop category="recipes" query="" pagination="false"]

How would I use the query part of that? What does it refer to?

Also, what does the pagination setting do?

Thanks!

Jan 15 2010 04:39

Hi John,
I too am having trouble with this shortcode working. I plugged it into the page as you mentioned and followed all other directions yet it doesn’t work.
As Ivan stated, how should the query=”’ syntax be?
All I can guess is the right details are not there. It only shows the actual name i’ve put in cat=

Thanks!

Jan 15 2010 15:48

By default the query is blank you can pass in a complex query if you want . See the WordPress documentation on using more complex query structures.

http://codex.wordpress.org/Template_Tags/query_posts

@KD did you put this in your functions.php file or make a plugin?

Apr 25 2010 14:15

I’m getting some problems with this code, both using it in a theme and using it in a plugin.

Warning: Cannot modify header information – headers already sent by (output started at /wordpress/wp-content/themes/cisv-leirtema/functions.php:50) in /wordpress/wp-includes/pluggable.php on line 868

It’s essentially making the admin section useless. Any ideas about what could be wrong?

The shortcode itself is working just fine and I’m able to post the part of the loop that I want to, the only problem is the warning I get.

Apr 25 2010 21:38

As noobtastic it may sound it turns out the problem was white space where it shouldn’t be and I’ve managed to fix the problem. Sorry for bothering you, and thank you for a nice piece of code.

May 25 2010 08:29

Great stuff here, any chance you know how to sort the results by title instead of post date?

May 25 2010 09:40

Nevermind, I just managed to figure it out. Thanks a bunch for this, we’re working on a few projects and I’m just messing around but this will come in handy for many different things :-)

If anyone else is interested here’s the snippet I modified:

Original:

if(!empty($query)){
$query .= $query;
}

Modified

if(!empty($query)){
$query .= ‘&orderby=title&order=ASC&tag=’ .$query;
}

Thanks again, very cool!

May 25 2010 14:50

@jeremia cool glad you got it figured out :)

Jul 28 2010 07:37

Sweet! Thank you for this demo code! How would I modify this to work with custom terms and custom page types.

Thanks again!

Jul 29 2010 18:09

I just wanted you to know: I was searching for “wordpress shortcode the loop” and found this post. I do not have PHP experience, but I was able to add and modify this snippet and get exactly what I wanted.

Thank you so much!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required