Last active
May 12, 2016 20:02
-
-
Save andykillen/92a3d27072fdd19336a0ddca571b2e97 to your computer and use it in GitHub Desktop.
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
$ids = array(); | |
// get current post id if a single (post or page) | |
if(is_single()){ | |
global $post; | |
$ids[] = $post->ID; | |
} | |
// setup basic loop arguments | |
$args = array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => 5 + count($ids), | |
'no_found_rows' => true, // turn off pagination | |
'update_post_meta_cache' => false // don't do anything with post meta | |
); | |
// 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 | |
// set a counter so we know when we get to 5 | |
$count = 0; | |
while ($loop->have_posts()) : $loop->the_post() ; | |
// check if this is not part of the already loaded ones | |
if(in_array(get_the_ID(), $ids)) { | |
continue; // miss out this one and begin a new loop | |
} | |
// do the things that are shown in the loop | |
?><li> | |
<a href='<?php the_permalink(); ?>'><?php the_title(); ?></a> | |
</li><?php | |
// add 1 to the loop count. | |
$count++; | |
// stop the loop when at 5 | |
if($count == 5){ | |
// end the loop | |
break; | |
} | |
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