{"id":409,"date":"2012-06-27T23:35:40","date_gmt":"2012-06-27T23:35:40","guid":{"rendered":"http:\/\/codebangers.com\/?p=409"},"modified":"2012-06-27T23:41:27","modified_gmt":"2012-06-27T23:41:27","slug":"wordpress-shortcode-to-display-the-loop","status":"publish","type":"post","link":"https:\/\/codebangers.com\/wordpress-shortcode-to-display-the-loop\/","title":{"rendered":"WordPress Shortcode to Display the Loop"},"content":{"rendered":"<p>Just paste this into your theme&#8217;s function file.<\/p>\n<p><code lang=\"php\"><\/p>\n<p><?\nfunction myLoop($atts, $content = null) {\n\textract(shortcode_atts(array(\n\t\t\"pagination\" => 'true',<br \/>\n\t\t\"query\" => '',<br \/>\n\t\t\"category\" => '',<br \/>\n\t), $atts));<br \/>\n\tglobal $wp_query,$paged,$post;<br \/>\n\t$temp = $wp_query;<br \/>\n\t$wp_query= null;<br \/>\n\t$wp_query = new WP_Query();<br \/>\n\tif($pagination == 'true'){<br \/>\n\t\t$query .= '&paged='.$paged;<br \/>\n\t}<br \/>\n\tif(!empty($category)){<br \/>\n\t\t$query .= '&category_name='.$category;<br \/>\n\t}<br \/>\n\tif(!empty($query)){<br \/>\n\t\t$query .= $query;<br \/>\n\t}<br \/>\n\t$wp_query->query($query);<br \/>\n\tob_start();<br \/>\n\t?><\/p>\n<h2><?php echo $category; ?><\/h2>\n<ul class=\"loop\">\n\t<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?><\/p>\n<li><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\"><?php echo $thumbnail_image; the_title(); ?><\/a><\/li>\n<p>\t<?php endwhile; ?>\n\t<\/ul>\n<p>\t<?php if(pagination == 'true'){ ?><\/p>\n<div class=\"navigation\">\n<div class=\"alignleft\"><?php previous_posts_link('\u00ab Previous') ?><\/div>\n<div class=\"alignright\"><?php next_posts_link('More \u00bb') ?><\/div>\n<\/p><\/div>\n<p>\t<?php } ?><br \/>\n\t<?php $wp_query = null; $wp_query = $temp;\n\t$content = ob_get_contents();\n\tob_end_clean();\n\treturn $content;\n}\nadd_shortcode(\"loop\", \"myLoop\");\n\n?><\/p>\n<p><\/code><br \/>\nOnce your functions.php file is saved, you can display a loop :<br \/>\n<code lang=\"php\"><br \/>\n[loop category=\"news\" query=\"\" pagination=\"false\"]<br \/>\n<\/code><\/p>\n<p>Note that this code have been created to been used in pages. It have some oddity when used in a post.<\/p>\n<p>Thanks to John Turner for this great piece of code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just paste this into your theme&#8217;s function file.<\/p>\n","protected":false},"author":2,"featured_media":12,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-409","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-wordpress"],"_links":{"self":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":6,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/409\/revisions\/415"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media\/12"}],"wp:attachment":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}