Skip to content

Instantly share code, notes, and snippets.

@andykillen
Last active August 16, 2016 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andykillen/8181f2442247e07799a1029b002735a2 to your computer and use it in GitHub Desktop.
Save andykillen/8181f2442247e07799a1029b002735a2 to your computer and use it in GitHub Desktop.
Standard WordPress loop
<?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