I finally have the shortcode that displays popular post cards with all of the meta data: thumbnail, title, categories, excerpt, avatar, display name, date and number of comments.
So, I’ll just leave it here in case someone needs it.
if (shortcode_exists('koko_analytics_most_viewed_posts')) {
add_shortcode('az_most_viewed_posts','popular_posts_334254');
function popular_posts_334254( $args, $posts ) {
$default_args = array(
'number' => 3,
'post_type' => 'post',
'show_date' => false,
'days' => 30,
);
$args = shortcode_atts( $default_args, $args );
$posts = KokoAnalytics\get_most_viewed_posts($args); // Adding KokoAnalytics/ because inside the original callback it is namespaced
$html .= '<div class="az-popular-posts">';
$html .= '<div class="az-popular-posts-inner">';
foreach ( $posts as $p ) {
$categories = '';
$category_detail=get_the_category($p);//$post->ID
foreach($category_detail as $cd){
$category_link = get_category_link($cd->cat_ID);
$categories = $categories. ' '.'<a href="'.esc_url( $category_link ).'">'.$cd->cat_name.'</a>';
}
$get_author = get_avatar( $id_or_email = get_the_author_meta( 'user_email' ), $size = '30', $default, $alt = 'Author avatar', $args = array( 'class' => array( 'avatar-below-title', 'inline-meta' ) ) );
$authorname = get_the_author();
$authorurl = get_author_posts_url($p);
$authordetails = '<a href="'.esc_url( $authorurl ).'" class="inline-meta author-name" >'.$authorname.'</a>';
$commentsurl = get_comments_link($p);
$commentnumber = get_comments_number($p);
$post_comments = '<a href="'.esc_url( $commentsurl ).'" comments_number="'.$commentnumber.'" class="blog-card-comments inline-meta">' .$commentnumber. '</a>';
$post_title = get_the_title( $p );
$title = $post_title !== '' ? $post_title : esc_html__( '(no title)', 'koko-analytics' );
$aria_current = '';
$authurthumb = get_the_post_thumbnail( $p, 'medium', ['alt' => $title], array( 'class' => 'blog-card-image' ));
$post_thumbnail = '<a href="'.esc_url( get_the_permalink( $p )).'" class="card-img-link">' .$authurthumb. '</a>';
if ( get_queried_object_id() === $p->ID ) {
$aria_current = ' aria-current="page"';
}
$html .= '<div class="universal-post-card">';
// POST THUMBNAIL
$html .= '<div class="blog-img-and-cat">';
$html .= sprintf($post_thumbnail);
// CATEGORIES
$html .='<div class="blog-card-cat-top">';
$html .= sprintf($categories);
$html .= '</div>';
$html .= '</div>';
// POST CARD INNER
$html .= '<div class="blog-card-grid">';
$html .= sprintf( '<h3 class="blog-card-title"><a href="%s" %s>%s</a></h3>', get_the_permalink( $p ), $aria_current, $title );
// POST EXCERRPT
$html .= '<p class="blog-card-excerpt">';
$html .= sprintf(get_the_excerpt( $p ));
$html .= '</p>';
/*
if ( $args['show_date'] ) {
$html .= sprintf( PHP_EOL . ' <span class="post-date wp-block-post-date">%s</span>', get_the_date( '', $p ) );
}
*/
// author avatar
$html .= sprintf($get_author);
// author name
$html .= sprintf($authordetails);
// post time
$html .= '<span class="blog-card-date inline-meta">';
$html .= sprintf(get_the_date( 'M d, Y', $p ));
$html .= '</span>';
// commnets
$html .= sprintf($post_comments);
$html .= '</div>';
$html .= '</div>';
}
$html .= '</div>';
$html .= '</div>';
return $html;
}
}