Download pdf - Add loop shortcode

Transcript
Page 1: Add loop shortcode

add_loop_shortcode.php 2011-11-06

- 1/2 -

<?php/*Plugin Name: Loop ShortcodePlugin URI: http://semperfiwebdesign.com/Description: Adds a [loop] shortcode for embedding The Loop.Version: 2.0Author: Semper Fi Web DesignAuthor 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">

Page 2: Add loop shortcode

add_loop_shortcode.php 2011-11-06

- 2/2 -

"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">

<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 ***/}?>


Recommended