This code is an example of post navigation for use specifically with Types & Views. The post navigation works well in tandem with View’s parametric search forms, and can output next/previous post links based on URL parameters that indicate custom taxonomies and fields.
You can see this code in action at: http://dev.rocketdogrescue.org/adopt/adoptees/. Enter some search criteria and then click on an individual dog to check out the post navigation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | <?php /** Next/prev links on single Dog posts */ functioncustom_post_nav(){ if(is_singular('dog')||is_singular('success-story')){?> <divclass="post-nav"> <divclass="container"> <?php /* Get current post ID */ $current_ID=get_the_ID(); /* Get current post type */ $post_type=get_post_type(); /* Get URL parameters */ $url_query=!empty($_SERVER['QUERY_STRING'])?'?'.$_SERVER['QUERY_STRING']:''; $url_params['status']=wp_kses_post($_GET['status']); $url_params['breed']=!empty($_GET['breed'])?wp_kses_post($_GET['breed']):''; $url_params['wpcf-dog-age']=wp_kses_post($_GET['age']); $url_params['wpcf-dog-sex']=wp_kses_post($_GET['sex']); $url_params['wpcf-dog-size']=wp_kses_post($_GET['size']); /* Query args - taxonomies go here (custom fields below) */ $args=array( 'post_type'=>$post_type, 'adoption-status'=>$url_params['status'], 'dog-breed'=>$url_params['breed'], ); /* Add custom field args for CPT 'dog' since these don't work for success stories */ if('dog'==$post_type){ // Only add non-empty custom fields and the specific custom fields we want foreach($url_paramsas$k=>$v){ if(strstr($k,"wpcf")&& !empty( $v ) ) { $args[ 'meta_query' ][] = array ( 'key' => $k, 'value' => $v ); } } } $query=newWP_Query($args); $i=0; while($query->have_posts()){ $query->the_post(); $id=get_the_ID(); // If we're on the current post if($id==$current_ID){ $next_post=$query->posts[$i-1]; $prev_post=$query->posts[$i+1]; } $i++; } wp_reset_postdata(); // Output next post if(!empty($next_post)){ echo'<a class="next-post" href="'.get_permalink($next_post->ID).$url_query.'">'.$next_post->post_title.'</a>'; } // Don't output adoption status parameter, but do save it for selecting the appropriate $links[] below $status=$url_params['status']; $status=!empty($status)?'-'.$status:''; $url_params['status']=''; // Get URL parameters and remove empty array items - done automatically with call to array_filter with no callback function $params=implode(', ',array_filter($url_params)); // Remove hyphens and capitalize $params=ucwords(str_replace('-',' ',$params)); // Top level links $links['dog']='/adopt/adoptees/'; $links['dog-for-adoption']='/adopt/adoptees/'; $links['dog-needs-foster-home']='/dogs-for-adoption-in-san-francisco-bay-area-rocket-dog-rescue-san-francisco/'; $links['success-story']='/adopt/success-stories/'; // Output middle post type link echo'<a class="all-dogs" href="'.$links[$post_type.$status].$url_query.'">'; echoget_post_type_object($post_type)->labels->name; echo!empty($params)?': '.$params:''; echo'</a>'; // Output pevious post if(!empty($prev_post)){ echo'<a class="prev-post" href="'.get_permalink($prev_post->ID).$url_query.'">'.$prev_post->post_title.'</a>'; } ?> </div> </div> <?php } } add_action('genesis_entry_header','custom_post_nav',0); |