Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$pagination_query = new WP_Query($args);
$posts_full = get_posts($args);
while ( $pagination_query->have_posts() ) {
$pagination_query->the_post();
$posts[] = get_the_ID();
}
$posts_reverse = array_reverse($posts);
$current_post = $post->ID;
$index = array_search($current_post, $posts);
$index_reverse = array_search($current_post, $posts_reverse);
$before = array_key_exists($index - 1, $posts) ? $index - 1 : null;
$after = array_key_exists($index + 1, $posts) ? $index + 1 : null;
$after_count = 3;
$before_count = 3;
$before_posts = array_reverse((array_slice( $posts_reverse, $index_reverse, $before_count, true)));
$after_posts = (array_slice($posts, $index, $after_count, true));
if ( count( $before_posts <= 3 ) ) {
$after_count += 3 - count( $before_posts );
}
if ( count( $after_posts <= 3 ) ) {
$before_count += 3 - count( $after_posts );
}
$before_posts = array_reverse((array_slice( $posts_reverse, $index_reverse, $before_count, true)));
$after_posts = (array_slice($posts, $index, $after_count, true));
$all_posts = array_merge($before_posts, array_slice($after_posts, 1));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.