Advertisement
Guest User

get_offset_post_link

a guest
Oct 11th, 2011
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. function get_offset_post_link($format, $link, $offset, $post_type, $order_by, $custom_query_array){
  2.     global $post;
  3.     $current_post_id = $post->ID;
  4.    
  5.     $list_query = New WP_Query(array(
  6.         'post_type'         => $post_type,
  7.         'orderby'           => $order_by,
  8.         'order'             => 'ASC',
  9.         'posts_per_page'    => -1,
  10.         'meta_query'        => $custom_query_array
  11.      ));   
  12.     $i = 0;
  13.  
  14.     if ( $list_query->have_posts() ) while ( $list_query->have_posts() ) : $list_query->the_post();
  15.        
  16.         $item[$i] = get_post($post->ID);
  17.        
  18.  
  19.         if($post->ID == $current_post_id){
  20.             $target_index = $i + $offset;
  21.             if($target_index >= 0){
  22.                 if($target_index <= $i){
  23.                     $target_post =  $item[$target_index];          
  24.                     break;
  25.                 }else{
  26.                     if($target_index <= $list_query->post_count){
  27.                         while($i < $target_index){
  28.                             $target_post = $list_query->next_post();
  29.                             if($i == $target_index){
  30.                                 break;
  31.                             }
  32.                             $i++;
  33.                         }
  34.                     }else{
  35.                         //offset exceeds the upper bounds of the record set.
  36.                     }
  37.                 }
  38.             }else{
  39.                 //offset exceeds the lower bounds of the record set.               
  40.             }
  41.         }
  42.         $i++;
  43.     endwhile;
  44.  
  45.  
  46.     wp_reset_query();
  47.     wp_reset_postdata();
  48.    
  49.     if($target_post){
  50.         $link = str_replace('%title', $target_post->post_title, $link);
  51.         $link = '<a href="' . get_permalink($target_post->ID) . '">' . $link . '</a>';
  52.        
  53.         $format = str_replace('%link', $link, $format);
  54.  
  55.         return $format;
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement