Last active
December 15, 2015 07:19
-
-
Save efarem/5222216 to your computer and use it in GitHub Desktop.
WordPress loop shortcode, super useful for when you're faking post type archives using pages for SEO or for adding content above and below easily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!class_exists('FRM_Loop')) | |
{ | |
class FRM_Loop | |
{ | |
public function __construct() | |
{ | |
add_shortcode('loop', array(&$this, 'loop_shortcode')); | |
} | |
/** | |
* Get a loop | |
* | |
* @return string | |
* @author Frank Martin | |
**/ | |
public function loop_shortcode($atts, $content = NULL) | |
{ | |
global $wp_query; | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
$args = shortcode_atts(array( | |
'post_type' => 'post', | |
'posts_per_page' => get_option('posts_per_page'), | |
'post_parent' => 0, | |
'paged' => $paged, | |
), $atts); | |
$temp = $wp_query; | |
$wp_query = NULL; | |
$wp_query = new WP_Query( $args ); | |
ob_start(); | |
if (have_posts()) : while (have_posts()) : the_post(); | |
get_template_part('content', $args['post_type']); | |
endwhile; endif; | |
$wp_query = NULL; | |
$wp_query = $temp; | |
wp_reset_query(); | |
return '<section class="loop">' . ob_get_clean() . '</section>'; | |
} | |
} | |
new FRM_Loop; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment