Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

get_comments

Function Description:

Retrieves a list of comments.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Get all comments for a specific post
$comments = get_comments( array(
    'post_id' => 123
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_content;
    }
} else {
    echo 'No comments found.';
}
// Example 2: Get comments with specific criteria
$comments = get_comments( array(
    'status' => 'approve',
    'number' => 5,
    'post_id' => 456
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo $comment->comment_author . ': ' . $comment->comment_content;
    }
} else {
    echo 'No approved comments found for post 456.';
}
// Example 3: Get comments for current post in a custom template
global $post;
$comments = get_comments( array(
    'post_id' => $post->ID
) );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '
'; echo '' . $comment->comment_author . ': ' . $comment->comment_content; echo '
'; } } else { echo 'No comments found for the current post.'; }