Nested Comments List [closed]

I have a wordpress site with 800 pages, each having a comment form. I need to get a central comments list that displays the comments in a nested format or it’s too hard to follow the thread.

I had ChatGPT write a script to do what I want, but when I run it in a browser, it only displays the structure, not the actual comments themselves. When I tell ChatGPT it doesn’t work, it gave me replacement routine but it’s still the exact same code.

This is the code:

<?php

function generateNestedCommentsList($comments, $parent_id = null) {
    echo '<ul>';

    foreach ($comments as $comment) {
        if ($comment['parent_id'] == $parent_id) {
            echo '<li>';
            echo $comment['text'];

            // Recursively call the function for nested comments
             generateNestedCommentsList($comments, $comment['id']);

             echo '</li>';
       }
    }

    echo '</ul>';
}

// Example usage:
$comments = array(
    array('id' => 1, 'parent_id' => null, 'text' => 'Comment 1'),
    array('id' => 2, 'parent_id' => 1, 'text' => 'Reply to Comment 1'),
    array('id' => 3, 'parent_id' => null, 'text' => 'Comment 2'),
    array('id' => 4, 'parent_id' => 2, 'text' => 'Reply to Reply 1'),
    // Add more comments as needed
);

generateNestedCommentsList($comments);
?>

This is the displayed results – and yes the database “id” is set to automatically increment – so the ids should not get confused:

  • Comment 1
    • Reply to Comment 1
    • Reply to Reply 1
  • Comment 2

TIA

  • Seems to work just fine here (though it’s ineffecient) ~ 3v4l.org/PocC7. What exactly is the problem with the output it produces?

    – 




  • The code only includes some example comments, it doesn’t fetch the actual comments from your database. That’s the part you need to do yourself.

    – 

  • 1

    I’m saying ChatGPT gave you code that formats data as a nested list. What it didn’t do is give you the code that fetches the actual data from your database. You are supposed to add this yourself to complete this code. Since this is a site for programmers, you’re kind of expected to be able to do this yourself, or at least to ask a specific question about what you’re stuck on doing so. “Here’s some ChatGPT code, please complete it for me” is not an acceptable question. Please see How to Ask.

    – 

  • 1

    So your question is something like how to fetch all comments in WordPress? Then why aren’t you asking that question?

    – 




  • 1

    …or if you don’t know how to get data from a database using wordpress, you could ask that, maybe. I agree with deceze, there’s no clear problem statement, and no evidence of any attempt to resolve this yourself. We’re happy to help where needed (without repeating ourselves) but as volunteers we do expect that you a) ask a clear and meaningful question so we don’t have to use 20 comments to get to the bottom of the issue and b) show a genuine attempt to look into it yourself and talk about it here. It’s actually quite hard, in 2023, to ask something completely new and unique.

    – 




Leave a Comment