Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Plugin Name: Custom Query Shortcode
  4.  * Plugin URI:  
  5.  * Description: Run a Loop inside any Post/Page via Shortcode <code>[loop]</code>
  6.  * Version:     0.0.1
  7.  * License:     GPLv3
  8.  * Author:      Frank B&uuml;ltge
  9.  * Author URI:  http://bueltge.de/
  10.  */
  11.  
  12.  // This file is not called from WordPress. We don't like that.
  13. ! defined( 'ABSPATH' ) and exit;
  14.  
  15. /**
  16.  * EXAMPLE USAGES:
  17.  * [loop]
  18.  * [loop  query="posts_per_page=3"]
  19.  * [loop the_query="showposts=15&post_type=post&post_parent=453"]
  20.  *
  21.  * @param  $before string Markup,content Before content of loop output
  22.  * @param  $after string Markup,content After content of loop output
  23.  * @param  http://codex.wordpress.org/Class_Reference/WP_Query
  24.  * @see    http://codex.wordpress.org/Class_Reference/WP_Query
  25.  */
  26. function fb_custom_query_shortcode( $atts ) {
  27.    
  28.    // Defaults
  29.    extract( shortcode_atts( array(
  30.       'the_query' => '',
  31.       'before' => '<ul>',
  32.       'after' => '</ul>'
  33.    ), $atts ) );
  34.  
  35.    // de-funkify query
  36.    $the_query = preg_replace( '~&#x0*([0-9a-f]+);~ei', 'chr( hexdec("\\1") )', $the_query );
  37.    $the_query = preg_replace( '~&#0*([0-9]+);~e', 'chr(\\1)', $the_query );
  38.  
  39.    // query is made              
  40.    query_posts( $the_query );
  41.    
  42.    // Reset and setup variables
  43.    $output = '';
  44.    $temp_title = '';
  45.    $temp_link = '';
  46.    
  47.    // the loop
  48.    if ( have_posts() ) : while ( have_posts() ) : the_post();
  49.    
  50.       $temp_title = get_the_title( $post->ID );
  51.       $temp_link = get_permalink( $post->ID );
  52.      
  53.       // output all findings - CUSTOMIZE TO YOUR LIKING
  54.       $output .= '<li><a href="' . $temp_link . '">' . $temp_title . '</a></li>';
  55.          
  56.    endwhile; else:
  57.    
  58.       $output .= __( 'nothing found.' );
  59.      
  60.    endif;
  61.    
  62.    wp_reset_query();
  63.    return $before . $output . $after;
  64.    
  65. }
  66. add_shortcode( 'loop', 'fb_custom_query_shortcode' );