Skip to content

Instantly share code, notes, and snippets.

@webmasterninjay
Created February 21, 2015 08:15
Show Gist options
  • Save webmasterninjay/3f2d8fa9ee46f4e5d1b1 to your computer and use it in GitHub Desktop.
Save webmasterninjay/3f2d8fa9ee46f4e5d1b1 to your computer and use it in GitHub Desktop.
WP function to create shortcode that display posts base on attribute
<?php
function jay_query_shortcode($atts) {
// Example usage of this shortcode:
// [loop the_query="showposts=100&post_type=page&post_parent=453"]
// Defaults
extract(shortcode_atts(array(
"the_query" => ''
), $atts));
// the query
$the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
$the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
// query is made
query_posts($the_query);
// Reset and setup variables
$output = '';
$temp_title = '';
$temp_link = '';
// the loop
if (have_posts()) : while (have_posts()) : the_post();
$temp_title = get_the_title($post->ID);
$temp_link = get_permalink($post->ID);
// output all findings - CUSTOMIZE TO YOUR LIKING
$output .= "<li><a href='$temp_link'>$temp_title</a></li>";
endwhile; else:
$output .= "nothing found.";
endif;
wp_reset_query();
return $output;
}
add_shortcode("jay_loop", "jay_query_shortcode");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment