Last active
August 16, 2016 06:42
-
-
Save andykillen/8181f2442247e07799a1029b002735a2 to your computer and use it in GitHub Desktop.
Standard WordPress loop
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 | |
$id = false; | |
// get current post id if a single (post or page) | |
if(is_single()){ | |
global $post; | |
$id = $post->ID; | |
} | |
// setup basic loop arguments | |
$args = array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => '5' | |
); | |
// add the id as an exclude to the args if there is one | |
if($id){ | |
$args['post__not_in'] = array($id); | |
} | |
// create the loop query | |
$loop = new WP_Query($args); | |
// check if has a good result | |
if ($loop->have_posts()) : | |
// begin the unordered list | |
?><ul><?php | |
while ($loop->have_posts()) : $loop->the_post() ; | |
// do the things that are shown in the loop | |
?><li> | |
<a href='<?php the_permalink(); ?>'><?php the_title(); ?></a> | |
</li><?php | |
endwhile; | |
// end the | |
?></ul><?php | |
wp_reset_postdata(); | |
else : | |
// give a response if there is no result | |
echo "there were no results.... that\'s strange! check your code."; | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment