Advertisement
supportnwl

creating shortcode to group location by region

Aug 30th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function em_location_by_region(){
  2. $args = array();
  3. $arr_location = EM_Locations::get($args);
  4.  
  5.  
  6. /*Get regions as header*/
  7. $arr_region = array();
  8. foreach ( $arr_location as $EM_Location_region ) {
  9. if ( !in_array($EM_Location_region->region,$arr_region) && !empty($EM_Location_region->region) ){
  10. array_push($arr_region,$EM_Location_region->region);
  11. }
  12. }
  13.  
  14. /*Get state*/
  15. $arr_state = array();
  16. $arr_region_state = array();
  17. foreach ( $arr_region as $key => $value ) {
  18. foreach ( $arr_location as $EM_Location_state ) {
  19. if ( $EM_Location_state->region == $value ){
  20. array_push($arr_state,'<a href="'.$EM_Location_state->get_permalink().'">'.$EM_Location_state->name.'</a>');
  21. }
  22. }
  23. $arr_region_state[$value] = $arr_state;
  24. if ( !empty($arr_state) )$arr_state = array();
  25. }
  26. ksort($arr_region_state);
  27. /*display the result*/
  28. foreach ( $arr_region_state as $key => $value ) {
  29. echo '<h2>'.$key.'</h2>';
  30. echo '<ul>';
  31. foreach ( $value as $em_state ) {
  32. echo '<li>'.$em_state.'</li>';
  33. }
  34. echo '</ul>';
  35. }
  36.  
  37. }
  38. add_shortcode('location_group', 'em_location_by_region');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement