Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'last-comments', 'last_comments_user' );
- function last_comments_user( $atts ) {
- global $wpdb, $user_ID;
- //if(!in_array($user_ID,array(1,44))) return false;
- extract( shortcode_atts( array(
- 'title' => 'Последние комментарии',
- 'class' => 'full'
- ), $atts ) );
- $commentIds = $wpdb->get_col(
- "SELECT "
- . "MAX(comments.comment_ID) AS comment_ID "
- . "FROM "
- . "$wpdb->posts AS posts "
- . "INNER JOIN $wpdb->comments AS comments ON posts.ID = comments.comment_post_ID "
- . "WHERE posts.post_status IN ('publish','inherit') "
- . "AND posts.post_type != 'task' "
- . "AND comments.comment_approved = '1' "
- . "AND comments.comment_type = '' "
- . "GROUP BY posts.ID "
- . "ORDER BY MAX(comments.comment_date) DESC "
- . "LIMIT 5"
- );
- if ( ! $commentIds )
- return false;
- $comments = $wpdb->get_results(
- "SELECT "
- . "comments.comment_ID,"
- . "comments.comment_post_ID,"
- . "comments.comment_content,"
- . "comments.user_id "
- . "FROM "
- . "$wpdb->comments AS comments "
- . "WHERE "
- . "comments.comment_ID IN (" . implode( ",", $commentIds ) . ") "
- . "ORDER BY comments.comment_date DESC"
- );
- if ( ! $comments )
- return false;
- $postIds = array();
- foreach ( $comments as $comment ) {
- $postIds[] = $comment->comment_post_ID;
- }
- $posts = $wpdb->get_results(
- "SELECT "
- . "posts.ID,"
- . "posts.post_title "
- . "FROM "
- . "$wpdb->posts AS posts "
- . "WHERE "
- . "posts.ID IN (" . implode( ",", $postIds ) . ")"
- );
- $postTitles = array();
- foreach ( $posts as $post ) {
- $postTitles[$post->ID] = $post->post_title;
- }
- $content = '<div class="last-comments-box posts-box ' . $class . '">
- <h3>' . $title . '</h3>
- <ul>';
- ob_start();
- foreach ( $comments as $comment ) {
- $commentUrl = get_permalink( $comment->comment_post_ID ) . "#comment-" . $comment->comment_ID;
- $commentText = wp_trim_words( $comment->comment_content, 17 );
- if ( ( iconv_strlen( $title = $postTitles[$comment->comment_post_ID], 'utf-8' ) > 30 ) ) {
- $title = iconv_substr( $title, 0, 30, 'utf-8' );
- $title = preg_replace( '@(.*)\s[^\s]*$@s', '\\1', $title ) . '...';
- }
- ?>
- <li>
- <a href="<?php echo $commentUrl; ?>"><?php echo get_avatar( $comment->user_id, 60 ); ?></a>
- <a class="post-title" href="<?php echo $commentUrl; ?>">
- <?php echo $title; ?>
- </a>
- <p><?php echo $commentText . ' <a href=' . $commentUrl . '> Читать далее</a>'; ?></p>
- </li>
- <?php
- }
- $content .= ob_get_contents();
- ob_end_clean();
- $content .= '</ul></div>';
- return $content;
- }
Add Comment
Please, Sign In to add comment