Instantly share code, notes, and snippets.
jmarreros/dcms_list_authors.php
Created Jan 5, 2021
Modificacion de la funcion wp_list_authors de WordPress para que muestre autores con imagen
<?php | |
function dcms_list_authors( $args = '' ) { | |
global $wpdb; | |
$defaults = array( | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'number' => '', | |
'optioncount' => false, | |
'exclude_admin' => true, | |
'show_fullname' => false, | |
'hide_empty' => true, | |
'feed' => '', | |
'feed_image' => '', | |
'feed_type' => '', | |
'echo' => true, | |
'style' => 'list', | |
'html' => true, | |
'exclude' => '', | |
'include' => '', | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$return = ''; | |
$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); | |
$query_args['fields'] = 'ids'; | |
$authors = get_users( $query_args ); | |
$author_count = array(); | |
foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ) as $row ) { | |
$author_count[ $row->post_author ] = $row->count; | |
} | |
foreach ( $authors as $author_id ) { | |
$posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0; | |
if ( ! $posts && $args['hide_empty'] ) { | |
continue; | |
} | |
$author = get_userdata( $author_id ); | |
if ( $args['exclude_admin'] && 'admin' === $author->display_name ) { | |
continue; | |
} | |
if ( $args['show_fullname'] && $author->first_name && $author->last_name ) { | |
$name = "$author->first_name $author->last_name"; | |
} else { | |
$name = $author->display_name; | |
} | |
if ( ! $args['html'] ) { | |
$return .= $name . ', '; | |
continue; // No need to go further to process HTML. | |
} | |
if ( 'list' === $args['style'] ) { | |
$return .= '<li>'; | |
} | |
$link = '<img src="' . get_avatar_url($author_id, ['size' => '16']) . '" style="border: none;"' . $name . ' />'; | |
$link .= sprintf( | |
'<a href="%1$s" title="%2$s">%3$s</a>', | |
get_author_posts_url( $author->ID, $author->user_nicename ), | |
/* translators: %s: Author's display name. */ | |
esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ), | |
$name | |
); | |
if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { | |
$link .= ' '; | |
if ( empty( $args['feed_image'] ) ) { | |
$link .= '('; | |
} | |
$link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"'; | |
$alt = ''; | |
if ( ! empty( $args['feed'] ) ) { | |
$alt = ' alt="' . esc_attr( $args['feed'] ) . '"'; | |
$name = $args['feed']; | |
} | |
$link .= '>'; | |
$link .= '</a>'; | |
if ( empty( $args['feed_image'] ) ) { | |
$link .= ')'; | |
} | |
} | |
if ( $args['optioncount'] ) { | |
$link .= ' (' . $posts . ')'; | |
} | |
$return .= $link; | |
$return .= ( 'list' === $args['style'] ) ? '</li>' : ', '; | |
} | |
$return = rtrim( $return, ', ' ); | |
if ( $args['echo'] ) { | |
echo $return; | |
} else { | |
return $return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment