- Define the Shortcode Function: The code begins by defining a custom shortcode function named
custom_selected_comments_shortcode
. This function will handle the shortcode rendering and output. - Parse Shortcode Attributes: Inside the function, the shortcode attributes are parsed using the
shortcode_atts
function. The shortcode supports a single attribute namedcomment_ids
, which is a comma-separated list of comment IDs provided by the user. - Explode Comment IDs: The comma-separated
comment_ids
are exploded into an array using theexplode
function. This array will be used to query and display the selected comments. - Query Selected Comments: The shortcode queries the selected comments using the
get_comments
function. It sets up thecomment__in
parameter to retrieve comments with the specified IDs and thestatus
parameter to get only approved comments. - Display the Comments: The shortcode then starts output buffering using
ob_start()
to capture the HTML output. If there are comments to display ($comments
is not empty), it creates an unordered list (<ul>
) with the classselected-comments
. - Inside the list, it loops through each comment using a
foreach
loop. For each comment, it retrieves the comment author’s name, content, date, and related post information.
- The author’s avatar is obtained using
get_avatar
. - The comment author’s name is displayed within the
<strong>
tag. - The comment date is formatted using the
get_comment_date
function. - The post title (linked to the post) is displayed along with the “Commented on” text and a permalink.
- The comment content is wrapped in a
<div>
with the classcomment-content
. - Output Buffer and Return: After looping through all comments, the buffered output is cleaned and returned using
ob_get_clean()
. This will ensure that the HTML generated within the buffer is returned as the final output of the shortcode function. - Add the Shortcode: Finally, the shortcode is registered using
add_shortcode
. The shortcode name is'custom_selected_comments'
, and it is associated with thecustom_selected_comments_shortcode
function. This allows users to use the shortcode by enclosing it in square brackets in their content, e.g.,[custom_selected_comments comment_ids="1,2,3"]
.
In summary, this code defines a shortcode that takes a list of comment IDs as input, queries the corresponding comments, and displays them with author avatars, comment dates, and post information in an HTML structure. This output can then be styled using CSS, and as previously explained, you can implement a carousel to display the comments in an interactive way.
// Define the shortcode
function custom_selected_comments_shortcode($atts) {
// Parse shortcode attributes
$atts = shortcode_atts(array(
'comment_ids' => '', // Comma-separated list of comment IDs
), $atts, 'custom_selected_comments');
// Explode the comma-separated comment IDs into an array
$comment_ids = explode(',', $atts['comment_ids']);
// Query the selected comments
$comments_args = array(
'comment__in' => $comment_ids,
'status' => 'approve', // Display only approved comments
);
$comments = get_comments($comments_args);
// Display the comments
ob_start();
if ($comments) {
echo '<ul class="selected-comments">';
foreach ($comments as $comment) {
$comment_author = esc_html($comment->comment_author);
$comment_content = esc_html($comment->comment_content);
$comment_date = esc_html(get_comment_date('', $comment->comment_ID));
$comment_post = get_post($comment->comment_post_ID);
$post_title = esc_html(get_the_title($comment_post));
echo '<li>';
echo '<div class="comment-details">';
echo '<div class="comment-avatar">' . get_avatar($comment->comment_author_email, 50) . '</div>';
echo '<div class="comment-info">';
echo '<strong>' . $comment_author . '</strong>';
echo '<span class="comment-date">' . $comment_date . '</span>';
echo '<span class="comment-post">Commented on: <a href="' . get_permalink($comment->comment_post_ID) . '">' . $post_title . '</a></span>';
echo '</div>'; // End comment-info
echo '</div>'; // End comment-details
echo '<div class="comment-content">' . $comment_content . '</div>';
echo '</li>';
}
echo '</ul>';
} else {
echo '<p>No selected comments to display.</p>';
}
return ob_get_clean();
}
add_shortcode('custom_selected_comments', 'custom_selected_comments_shortcode');
Paste this code in you customer plugin or in Functions.php
Here is the short code:
[custom_selected_comments comment_ids="1,2,3"]
Also add your customer CSS
Leave a Reply