skip to Main Content

Many searches and found nothing similar!
I get Some comments on my home page as customer testimonials with code below. and I have two questions.

1- how can I trim comments from for example 60 words above.(cause some of the comment words are higher!)

2-how to get just comments less than 60 words?

                <?php
            $comments = get_comments(array(
                'page_id' => 2556,
                'status' => 'approve',
            ));
            
            //Display the list of comments
            wp_list_comments(array(
                'per_page' => 10,
                'avatar_size' => 30,
                'reverse_top_level' => true,
            ), $comments);

            ?>

2

Answers


  1. Chosen as BEST ANSWER

    I found answer for my question NO.1

    enter code here
                <?php
                $comments_query = new WP_Comment_Query;
                $comments = $comments_query->query(array(
                    'post_id' => 2556,
                    'status' => 'approve',
                    'number' => 10
                )); ?>
    
                <?php foreach ($comments as $comment) : ?>
                    <div class="custom-comment">
                    <p> <?php echo wp_trim_words($comment->comment_content, 60, '...') ?> </p>
                    <div class="custom-comment-author">
                        <span><?php echo $comment->comment_author ?></span>
                        <img src="<?php echo get_avatar_url($comment->comment_author_email); ?>" class="custom-avatar" loading="lazy" alt="">
                    </div>
                    </div>
                <?php endforeach; ?>
    

    I will answer if I find any solution of NO.2 but If anyone can answer, it would be amazing!


  2. <?php
    echo wp_trim_words( get_the_content(), 40, '...' );
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search