Skip to content

Instantly share code, notes, and snippets.

@johnmorris
Created April 29, 2013 14:11
Show Gist options
  • Save johnmorris/5481789 to your computer and use it in GitHub Desktop.
Save johnmorris/5481789 to your computer and use it in GitHub Desktop.
How to Create a Custom Loop in WordPress Using WP_Query
<?php
/*** Custom Loop ***/
function demo_loop() {
$args = array(
'cat' => 3,
'posts_per_page' => 1
);
$demo_posts = new WP_Query($args);
if ( $demo_posts->have_posts() ) {
while( $demo_posts->have_posts() ) {
$demo_posts->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
}
return $output;
}
add_shortcode( 'demo_custom_loop', 'demo_loop' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment