Skip to content

Instantly share code, notes, and snippets.

@iWebbers
Created August 11, 2018 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iWebbers/f05b17c165e6a6d747cc9bbfece81901 to your computer and use it in GitHub Desktop.
Save iWebbers/f05b17c165e6a6d747cc9bbfece81901 to your computer and use it in GitHub Desktop.
// Get WordPress recent comments via shortcode: [iw_recent_comments]
function iw_recent_comments_shortcode( $atts ) {
extract( shortcode_atts( array(
'author__not_in' => '',
'number' => '8',
'order' => 'DESC',
'avatar' => 'show',
'author' => 'show',
'date' => 'show',
'object' => 'show',
'excerpt' => 'show'
), $atts ) );
$args = array(
'author__not_in' => $author__not_in,
'number' => $number,
'order' => $order,
'status' => 'approve'
);
$comments = get_comments( $args );
$content = '';
$content .= '<div uk-slider="finite: true">';
$content .= '<div class="uk-position-relative uk-visible-toggle">';
$content .= '<ul class="uk-slider-items uk-child-width-1-2@m uk-grid uk-grid-match">';
foreach( $comments as $comment ) {
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
$content .= '<li>';
$content .= '<div class="uk-card uk-card-default uk-card-body">';
$content .= '<div class="uk-grid-small uk-flex-middle" uk-grid>';
$content .= '<div class="uk-width-auto">';
if ( $avatar == 'show' ) {
$content .= get_avatar( $comment, 60 );
}
$content .= '</div>';
$content .= '<div class="uk-width-expand">';
$content .= '<div class="uk-grid-collapse" uk-grid>';
if ( $author == 'show' ) {
$content .= '<div class="uk-width-expand">';
$content .= '<h3 class="uk-card-title uk-margin-remove-bottom uk-text-truncate">' . $comment->comment_author . '</h3>';
if ( $date == 'show' ) {
$content .= '<p class="uk-text-meta uk-margin-remove-top">' . mysql2date(get_option('date_format'), $comment->comment_date) . '</p>';
}
$content .= '</div>';
$content .= '<div class="uk-width-auto">';
if ($rating != 0)
{
$content .= '<h3 class="uk-card-title"><div style="margin-top: -2px">';
foreach (array(1,2,3,4,5) as $val)
{
$size = '20';
$score = $rating - $val;
if ($score >= 0)
{
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1728 647q0 22-26 48l-363 354 86 500q1 7 1 20 0 21-10.5 35.5t-30.5 14.5q-19 0-40-12l-449-236-449 236q-22 12-40 12-21 0-31.5-14.5t-10.5-35.5q0-6 2-20l86-500-364-354q-25-27-25-48 0-37 56-46l502-73 225-455q19-41 49-41t49 41l225 455 502 73q56 9 56 46z" fill="#f7c725"></path></svg>';
} else if ($score > -1 && $score < 0) {
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1250 957l257-250-356-52-66-10-30-60-159-322v963l59 31 318 168-60-355-12-66zm452-262l-363 354 86 500q5 33-6 51.5t-34 18.5q-17 0-40-12l-449-236-449 236q-23 12-40 12-23 0-34-18.5t-6-51.5l86-500-364-354q-32-32-23-59.5t54-34.5l502-73 225-455q20-41 49-41 28 0 49 41l225 455 502 73q45 7 54 34.5t-24 59.5z" fill="#f7c725"></path></svg>';
} else {
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1201 1004l306-297-422-62-189-382-189 382-422 62 306 297-73 421 378-199 377 199zm527-357q0 22-26 48l-363 354 86 500q1 7 1 20 0 50-41 50-19 0-40-12l-449-236-449 236q-22 12-40 12-21 0-31.5-14.5t-10.5-35.5q0-6 2-20l86-500-364-354q-25-27-25-48 0-37 56-46l502-73 225-455q19-41 49-41t49 41l225 455 502 73q56 9 56 46z" fill="#ccc"></path></svg>';
}
}
$content .= '</div></h3>';
}
$content .= '</div>';
}
$content .= '</div>';
$content .= '</div>';
$content .= '<blockquote>';
if ( $excerpt == 'show' ) {
$content .= get_comment_excerpt( $comment->comment_ID );
}
if ( $object == 'show' ) {
$content .= '<footer><a href="' . esc_url( get_permalink( $comment->comment_post_ID ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a></footer>';
}
$content .= '</blockquote>';
$content .= '</div>';
$content .= '</div>';
$content .= '</li>';
}
$content .= '</ul>';
$content .= '</div>';
$content .= '<ul class="uk-slider-nav uk-dotnav uk-flex-center uk-margin"></ul>';
$content .= '</div>';
return $content;
}
add_shortcode( 'iw_recent_comments', 'iw_recent_comments_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment