From 4e98304d06f90d1012577034043bd6105cefdbaa Mon Sep 17 00:00:00 2001 From: szk11001 Date: Wed, 11 Nov 2015 15:13:37 -0500 Subject: [PATCH] People Sorting in Widget & Clean URL's * Sort people in tables and grids by last name then first name * Change URL structure for viewing a person to base_url/person/firstname-lastname --- uc-people-widget.php | 117 ++++++++++++++++++++++++------------------- uc-people.php | 4 +- 2 files changed, 69 insertions(+), 52 deletions(-) diff --git a/uc-people-widget.php b/uc-people-widget.php index ede4233..8e265cc 100644 --- a/uc-people-widget.php +++ b/uc-people-widget.php @@ -14,25 +14,27 @@ Class UC_People_Widget extends WP_Widget { $out = '
'; $person_count = 1; - while( $the_query->have_posts() ){ - $the_query->the_post(); - $out .= '
'; + $people = $the_query->get_posts(); + usort( $people, array('UC_People_Widget', 'sort_by_last_then_first') ); + + foreach( $people as $id ){ + $out .= '
'; $out .= '
'; - $out .= ''; + $out .= ''; if (in_array('photo', $information_to_display)) { - $out .= '
'.get_the_post_thumbnail('large').'
'; + $out .= '
'.get_the_post_thumbnail( $id, 'large').'
'; }; if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){ $out .= '

'; if (in_array('first_name', $information_to_display)) { - $out .= get_field('first_name').' '; + $out .= get_field('first_name', $id ).' '; }; if (in_array('middle_name', $information_to_display)) { - $out .= get_field('middle_name').' '; + $out .= get_field('middle_name', $id ).' '; }; if (in_array('last_name', $information_to_display)) { - $out .= get_field('last_name'); + $out .= get_field('last_name', $id ); }; $out .= '

'; }; @@ -40,15 +42,15 @@ Class UC_People_Widget extends WP_Widget { foreach( $this->fields as $f ){ if( $f == 'file' && in_array('file', $information_to_display) ){ - $file = get_field('file'); + $file = get_field('file', $id ); $out .= '

'.$file[title].''.'

'; } elseif( $f == 'email' && in_array( 'email', $information_to_display ) ){ - $out .= '

'.get_field('email').''.'

'; + $out .= '

'.get_field('email', $id ).''.'

'; } else if( $f == 'title' && in_array( 'title', $information_to_display ) ){ - $out .= '

'.get_field('title').'

'; + $out .= '

'.get_field('title', $id ).'

'; } else { if ( in_array($f, $information_to_display) ) { - $out .= '

'.get_field($f).'

'; + $out .= '

'.get_field($f, $id ).'

'; }; } } @@ -60,7 +62,7 @@ Class UC_People_Widget extends WP_Widget { echo '
'; } $person_count++; - } //end of while posts + } $out .= '
'; // div.row } else { @@ -69,18 +71,31 @@ Class UC_People_Widget extends WP_Widget { return $out; } - private function createTextCell( $field, $information_to_display ){ + private static function sort_by_last_then_first($a,$b) { + setlocale(LC_CTYPE, 'en_US.UTF8'); + $r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $b)) ); + if( $r === 0 ){ + $r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $b)) ); + } + return $r; + } + + private function createTextCell( $field, $information_to_display, $id ){ $cell = ''; if (in_array($field, $information_to_display)) { - $cell .= ''.get_field($field).''; + $cell .= ''.get_field($field, $id ).''; } return $cell; } private function get_people_table( $args, $information_to_display ){ $the_query = new WP_Query( $args ); + $out = ''; if ( $the_query->have_posts() ) { + $people = $the_query->get_posts(); + usort( $people, array('UC_People_Widget', 'sort_by_last_then_first') ); + // how many columns? Based on what they chose to display. $num_cols = count($information_to_display); @@ -106,41 +121,39 @@ Class UC_People_Widget extends WP_Widget { $out .= ''; $out .= ''; - while ( $the_query->have_posts() ) { - $the_query->the_post(); - - $out .= ''; - if (in_array('photo', $information_to_display)) { - $out .= ''.the_post_thumbnail(array('65', '65')).''; - } - if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){ - $out .= ''; - if (in_array('first_name', $information_to_display)) { - $out .= get_field('first_name'); - $out .= ' '; - }; - if (in_array('middle_name', $information_to_display)) { - $out .= get_field('middle_name'); - $out .= ' '; - }; - if (in_array('last_name', $information_to_display)) { - $out .= get_field('last_name'); - }; - $out .= ''; - } + foreach( $people as $id ){ + $out .= ''; + if (in_array('photo', $information_to_display)) { + $out .= ''.get_the_post_thumbnail($id, array('65', '65')).''; + } + if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){ + $out .= ''; + if (in_array('first_name', $information_to_display)) { + $out .= get_field('first_name', $id ); + $out .= ' '; + }; + if (in_array('middle_name', $information_to_display)) { + $out .= get_field('middle_name', $id ); + $out .= ' '; + }; + if (in_array('last_name', $information_to_display)) { + $out .= get_field('last_name', $id ); + }; + $out .= ''; + } - foreach( $this->fields as $f ){ - if( $f == 'file' && in_array('file', $information_to_display) ){ - $file = get_field('file'); - $out .= ''.$file[title].''; - } else if( $f == 'email' && in_array('email', $information_to_display) ){ - $out .= ''.get_field('email').''; - } else { - $out .= $this->createTextCell($f, $information_to_display); - } + foreach( $this->fields as $f ){ + if( $f == 'file' && in_array('file', $information_to_display) ){ + $file = get_field('file', $id ); + $out .= ''.$file[title].''; + } else if( $f == 'email' && in_array('email', $information_to_display) ){ + $out .= ''.get_field('email', $id ).''; + } else { + $out .= $this->createTextCell($f, $information_to_display, $id ); } - $out .= ''; - } //end of posts + } + $out .= ''; + } $out .= ''; $out .= ''; } else { @@ -158,9 +171,11 @@ Class UC_People_Widget extends WP_Widget { $wpQueryArgs = array( 'posts_per_page' => -1, 'post_type' => 'person', - 'orderby' => 'menu_order title', - 'order' => 'ASC', - 'post__in' => ( ( !is_null($settings['specific_people']) ) ? $settings['specific_people'] : array() ) + //'orderby' => 'menu_order title', + //'order' => 'ASC', + //'order' => 'post__in', + 'post__in' => ( ( !is_null($settings['specific_people']) ) ? $settings['specific_people'] : array() ), + 'fields' => 'ids' ); if ( $settings['specific_groups'] != false || $settings['specific_tags'] != false ){ diff --git a/uc-people.php b/uc-people.php index e86d172..072c914 100644 --- a/uc-people.php +++ b/uc-people.php @@ -34,7 +34,9 @@ function create_custom_custom_post_type(){ 'menu_icon' => 'dashicons-groups', 'supports' => array( 'tags', 'thumbnail', 'revisions', 'page-attributes', 'author' ), 'has_archive' => true, - 'rewrite' => false + 'rewrite' => array( + 'slug' => '/person' + ) ); register_post_type( 'person', $args );