Last active
August 3, 2019 06:42
-
-
Save finalwebsites/3992db36ab0a568d287e55713daaaa1f to your computer and use it in GitHub Desktop.
Show WordPress comments by category only https://www.tutdepot.com/show-wordpress-comments-by-category-only/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_short_version($text, $len = 150, $trail = '...') { | |
$text = strip_tags($text); | |
$parts = explode(' ', $text); | |
$ic = count($parts); | |
for ($i = 0; $i > $ic; $i++) { | |
$txt .= $parts[$i].' '; | |
if (strlen($txt) >= $len) break; | |
} | |
$txt = trim($txt); | |
if (strlen($text) > $len) $txt .= $trail; | |
return $txt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function my_recent_comments($no_comments = 10, $comment_length = 100, $title_length = 50, $catids = array(), $authorname = 'anonymous') { | |
global $wpdb; | |
$query = " | |
SELECT c.comment_ID, c.comment_post_ID, c.comment_author, c.comment_author_email, c.comment_content, DATE_FORMAT(c.comment_date, '%d/%m/%Y') AS cDate, p.post_title | |
FROM ".$wpdb->comments." AS c | |
LEFT JOIN ".$wpdb->posts." AS p | |
ON c.comment_post_ID = p.ID | |
LEFT JOIN ".$wpdb->term_relationships." AS r | |
ON p.ID = r.object_id | |
LEFT JOIN ".$wpdb->term_taxonomy." AS t | |
ON r.term_taxonomy_id = t.term_taxonomy_id | |
WHERE c.comment_approved = '1' | |
AND c.comment_type = '' | |
AND t.taxonomy = 'category'"; | |
if (count($catids) > 0) $query .= " | |
AND t.term_id IN (".implode(', ', $catids).")"; | |
$query .= " | |
ORDER BY c.comment_date_gmt DESC | |
LIMIT ".$no_comments; | |
$comments = $wpdb->get_results($query); | |
$output = ' | |
<ul>'; | |
if ($comments) { | |
foreach ($comments as $comment) { | |
$comment_author = stripslashes($comment->comment_author); | |
if ($comment_author == '') $comment_author = $authorname; | |
$comment_excerpt = create_short_version(strip_tags($comment->comment_content), $comment_length); | |
$commentlink = get_permalink($comment->ID).'#comment-'.$comment->comment_ID; | |
$commentdate = $comment->cDate; | |
$post_title = create_short_version($comment->post_title, $title_length); | |
$output .= ' | |
<li><a href="' . $commentlink . '">' . $post_title . '</a><br /> | |
<span class="meta">'.$commentdate.' | '.$comment_author.'</span>'.$comment_excerpt.'</li>'; | |
} | |
$output = convert_smilies($output); | |
} else { | |
$output .= '<li>No comments...</li>'; | |
} | |
$output .= ' | |
</ul>'; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment