Last active
October 7, 2015 14:38
-
-
Save dragunoff/3180818 to your computer and use it in GitHub Desktop.
[WP] Query posts and list them using a template
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 | |
/* | |
Plugin Name: The Loop | |
Plugin URI: https://gist.github.com/3180818 | |
Description: Query posts and display them. | |
Version: 0.2 | |
Author: Ivaylo Draganov | |
Author URI: http://druuf.com/ | |
*/ | |
/** | |
* Query posts and display them using template parts. | |
* | |
* @author Ivaylo Draganov <iv.draganov@druuf.com> | |
* @param (array) $query_args - WP_Query arguments | |
* @param (array|string) $template_names - template path(s) and file name(s) relative to the theme dir | |
* @param (boolean) $echo | |
* @uses query_posts | |
* @uses locate_template | |
**/ | |
function custom_loop( $query_args = array(), $template_names = '', $echo = true) { | |
// default args | |
$defaults = array(); | |
// Parse incomming $args into an array and merge it with $defaults | |
$query_args = wp_parse_args( $query_args, $defaults ); | |
// Get posts from certain user | |
if ( isset( $query_args['author'] ) ) { | |
if( 'current' == $query_args['author'] ) { | |
global $user_ID; | |
$query_args['author'] = $user_ID; | |
} else if ( is_numeric( $query_args['author'] ) ) { | |
$query_args['author'] = intval( $query_args['author'] ); | |
} else { | |
unset($query_args['author']); | |
} | |
} | |
// Get children of current post | |
if ( isset( $query_args['post_parent'] ) ) { | |
if( 'current' == $query_args['post_parent'] ) { | |
global $post; | |
$query_args['post_parent'] = $post->ID; | |
} else if ( is_numeric( $query_args['post_parent'] ) ) { | |
$query_args['post_parent'] = intval( $query_args['post_parent'] ); | |
} else { | |
unset($query_args['post_parent']); | |
} | |
} | |
// The Template | |
$template_names = (array) $template_names; | |
$template_names[] = 'content.php'; | |
// The Query | |
query_posts( $query_args ); | |
// buffer output | |
ob_start(); | |
// The Loop | |
while ( have_posts() ) : the_post(); | |
// Locate and load the template | |
locate_template( $template_names, true, false ); | |
endwhile; | |
// get output buffer | |
$output = ob_get_clean(); | |
// Reset Query | |
wp_reset_query(); | |
// Give it to the world | |
if ( $echo ) { | |
echo $output; | |
} else { | |
return $output; | |
} | |
} | |
/** | |
* A shortcode using the above function | |
* | |
* @note output buffer used to capture and return the results of the WP loop | |
*/ | |
add_shortcode( 'theloop', 'custom_loop_shortcode' ); | |
function custom_loop_shortcode( $atts ) { | |
// EXAMPLE USAGE: | |
// [theloop query="showposts=100&post_type=page&post_parent=453" template="content-custom.php"] | |
// Defaults | |
$atts = shortcode_atts( array( | |
'query' => '', | |
'template' => '' | |
), $atts ); | |
// de-funkify query | |
$atts['query'] = html_entity_decode( $atts['query'] ); | |
// call our neat function | |
$out = custom_loop( $atts['query'], $atts['template'], false ); | |
// return it, phew... | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment