Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_offset_post_link($format, $link, $offset, $post_type, $order_by, $custom_query_array){
- global $post;
- $current_post_id = $post->ID;
- $list_query = New WP_Query(array(
- 'post_type' => $post_type,
- 'orderby' => $order_by,
- 'order' => 'ASC',
- 'posts_per_page' => -1,
- 'meta_query' => $custom_query_array
- ));
- $i = 0;
- if ( $list_query->have_posts() ) while ( $list_query->have_posts() ) : $list_query->the_post();
- $item[$i] = get_post($post->ID);
- if($post->ID == $current_post_id){
- $target_index = $i + $offset;
- if($target_index >= 0){
- if($target_index <= $i){
- $target_post = $item[$target_index];
- break;
- }else{
- if($target_index <= $list_query->post_count){
- while($i < $target_index){
- $target_post = $list_query->next_post();
- if($i == $target_index){
- break;
- }
- $i++;
- }
- }else{
- //offset exceeds the upper bounds of the record set.
- }
- }
- }else{
- //offset exceeds the lower bounds of the record set.
- }
- }
- $i++;
- endwhile;
- wp_reset_query();
- wp_reset_postdata();
- if($target_post){
- $link = str_replace('%title', $target_post->post_title, $link);
- $link = '<a href="' . get_permalink($target_post->ID) . '">' . $link . '</a>';
- $format = str_replace('%link', $link, $format);
- return $format;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement