Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
szk11001 committed Nov 11, 2015
1 parent a282f7b commit 4e98304
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 52 deletions.
117 changes: 66 additions & 51 deletions uc-people-widget.php
Expand Up @@ -14,41 +14,43 @@ Class UC_People_Widget extends WP_Widget {
$out = '<div class="row">';
$person_count = 1;

while( $the_query->have_posts() ){
$the_query->the_post();
$out .= '<div class="col-sm-3 personcount-'.$person_count.'" id="person-'.get_the_id().'">';
$people = $the_query->get_posts();
usort( $people, array('UC_People_Widget', 'sort_by_last_then_first') );

foreach( $people as $id ){
$out .= '<div class="col-sm-3 personcount-'.$person_count.'" id="person-'.$id.'">';
$out .= '<div class="person">';

$out .= '<a href="'.get_the_permalink().'" class="person-permalink">';
$out .= '<a href="'.get_the_permalink( $id ).'" class="person-permalink">';
if (in_array('photo', $information_to_display)) {
$out .= '<div class="person-image">'.get_the_post_thumbnail('large').'</div>';
$out .= '<div class="person-image">'.get_the_post_thumbnail( $id, 'large').'</div>';
};
if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){
$out .= '<h4 class="person-name">';
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 .= '</h4>';
};
$out .= '</a>';

foreach( $this->fields as $f ){
if( $f == 'file' && in_array('file', $information_to_display) ){
$file = get_field('file');
$file = get_field('file', $id );
$out .= '<p class="person-file"><a href="'.$file[url].'">'.$file[title].'</a>'.'</p>';
} elseif( $f == 'email' && in_array( 'email', $information_to_display ) ){
$out .= '<p class="person-email"><a href="mailto:'.get_field('email').'">'.get_field('email').'</a>'.'</p>';
$out .= '<p class="person-email"><a href="mailto:'.get_field('email', $id ).'">'.get_field('email', $id ).'</a>'.'</p>';
} else if( $f == 'title' && in_array( 'title', $information_to_display ) ){
$out .= '<p class="person-title"><strong>'.get_field('title').'</strong></p>';
$out .= '<p class="person-title"><strong>'.get_field('title', $id ).'</strong></p>';
} else {
if ( in_array($f, $information_to_display) ) {
$out .= '<p class="person-'.$f.'">'.get_field($f).'</p>';
$out .= '<p class="person-'.$f.'">'.get_field($f, $id ).'</p>';
};
}
}
Expand All @@ -60,7 +62,7 @@ Class UC_People_Widget extends WP_Widget {
echo '</div><!-- /row --><div class="row">';
}
$person_count++;
} //end of while posts
}
$out .= '</div>'; // div.row
} else {

Expand All @@ -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 .= '<td class="person-'.$field.'">'.get_field($field).'</td>';
$cell .= '<td class="person-'.$field.'">'.get_field($field, $id ).'</td>';
}
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);

Expand All @@ -106,41 +121,39 @@ Class UC_People_Widget extends WP_Widget {
$out .= '</tr></thead>';
$out .= '<tbody>';

while ( $the_query->have_posts() ) {
$the_query->the_post();

$out .= '<tr>';
if (in_array('photo', $information_to_display)) {
$out .= '<td class="person-image"><a href="'.get_the_permalink().'">'.the_post_thumbnail(array('65', '65')).'</a></td>';
}
if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){
$out .= '<td class="person-name"><a href="'.get_the_permalink().'">';
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 .= '</a></td>';
}
foreach( $people as $id ){
$out .= '<tr>';
if (in_array('photo', $information_to_display)) {
$out .= '<td class="person-image"><a href="'.get_the_permalink( $id ).'">'.get_the_post_thumbnail($id, array('65', '65')).'</a></td>';
}
if (in_array('first_name', $information_to_display) || in_array('middle_name', $information_to_display) || in_array('last_name', $information_to_display)){
$out .= '<td class="person-name"><a href="'.get_the_permalink( $id ).'">';
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 .= '</a></td>';
}

foreach( $this->fields as $f ){
if( $f == 'file' && in_array('file', $information_to_display) ){
$file = get_field('file');
$out .= '<td class="person-file"><a href="'.$file[url].'">'.$file[title].'</a>';
} else if( $f == 'email' && in_array('email', $information_to_display) ){
$out .= '<td class="person-email"><a href="mailto:'.get_field('email').'">'.get_field('email').'</a></td>';
} 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 .= '<td class="person-file"><a href="'.$file[url].'">'.$file[title].'</a>';
} else if( $f == 'email' && in_array('email', $information_to_display) ){
$out .= '<td class="person-email"><a href="mailto:'.get_field('email', $id ).'">'.get_field('email', $id ).'</a></td>';
} else {
$out .= $this->createTextCell($f, $information_to_display, $id );
}
$out .= '</tr>';
} //end of posts
}
$out .= '</tr>';
}
$out .= '</tbody>';
$out .= '</table>';
} else {
Expand All @@ -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 ){
Expand Down
4 changes: 3 additions & 1 deletion uc-people.php
Expand Up @@ -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 );
Expand Down

0 comments on commit 4e98304

Please sign in to comment.