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!

15 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?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required