More Related Content
Similar to Add loop shortcode
Similar to Add loop shortcode (20)
More from Peter Baylies (8)
Add loop shortcode
- 1. add_loop_shortcode.php 2011-11-06
<?php
/*
Plugin Name: Loop Shortcode
Plugin URI: http://semperfiwebdesign.com/
Description: Adds a [loop] shortcode for embedding The Loop.
Version: 2.0
Author: Semper Fi Web Design
Author URI: http://semperfiwebdesign.com/
*/
add_shortcode( 'loop', 'pdb_sfwd_loop' );
function pdb_sfwd_loop( $atts, $content = null ) {
$atts = shortcode_atts( array( /*** filter shortcode options ***/
"pagination" => 'true',
"query" => '',
"category_name" => '',
"post_type" => '',
"order" => '',
"orderby" => '',
"meta_key" => ''
), $atts );
$the_query = new WP_Query();
if( $atts['pagination'] == 'true' )
$atts['paged'] = get_query_var('paged');
unset($atts['pagination']);
$the_query->query( http_build_query( $atts ) );
ob_start(); /*** output buffering for convenience
***/
while ( $the_query->have_posts() ) : $the_query->the_post();
$id = $the_query->post->ID; /*** allow use of id variable ***/
$content = str_replace( '$id', "$id", $content );
echo do_shortcode ( $content ); /*** run shortcodes in the loop ***/
endwhile;
if( $pagination == 'true' ) { ?>
<div class="navigation">
- 1/2 -
- 2. add_loop_shortcode.php 2011-11-06
<div class="alignleft"><?php previous_posts_link( '« Previous' )
?></div>
<div class="alignright"><?php next_posts_link( 'More »',
$the_query->max_num_pages ) ?></div>
</div><?php
}
wp_reset_postdata();
return ob_get_clean(); /*** return output buffer contents ***/
}
?>
- 2/2 -