Created
April 29, 2013 14:11
-
-
Save johnmorris/5481789 to your computer and use it in GitHub Desktop.
How to Create a Custom Loop in WordPress Using WP_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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