0

I want my comment results to put inside anoher result.

I want this result

        $args = array(
                    'post_id'   =>$post->ID,
                    'status'  => 'approve',
                    'type'  => 'review_comment',
                );
                $comments = get_comments( $args );
                $abb = $comment->comment_content;
                $acc = get_comment_meta($comment->comment_ID, COMMENT_TITLE_METAKEY, true);
                $add = get_comment_meta($comment->comment_ID, COMMENT_RATING_METAKEY, true);
                $aee = get_comment_date( 'd\/m\/Y' );
                $aff = get_the_title();
                $agg = $comment->comment_author;
       $comlist = "review" => [
            "@type" => "Review",
            "headline" => $acc,
            "reviewbody" => $abb,
            "datePublished" => $aee,
            "itemReviewed" => [
                "@type" => "Organization",
                "name" => $aff,
                ],
            "author " => [
                "@type" => "Person",
                "name" => $agg,
                ],  
        ];

                $comlistall = foreach ( $comments as $comment ){

                     $comlist ;

                };

to put inside this result.

    <?php
    $rtitlez = get_the_title();
    $rimgz = get_the_post_thumbnail_url(get_the_ID(), 'full');
    $rvaluez = get_post_meta($post->ID, 'comments_rating_value', true);
    $rcountz = get_post_meta($post->ID, 'comments_rating_count', true);
 $result = [
  "@context" => "https://schema.org/",
  "@type" => "Organization",
  "name" => $rtitlez,
  "image" => $rimgz,
  "aggregateRating" => [
        "@type" => "AggregateRating",
        "ratingValue" => $rvaluez,
        "bestRating" => "5",
        "worstRating" => "1",
        "ratingCount" => $rcountz
      ],
    /*I want here $comlistall*/

  ]
  ;?>                       
<script type="application/ld+json"><?php echo json_encode($result); ?></script>

How do I do that?

1 Answer 1

1

Run the loop in advance and populate an array, which in turn you include in the $result afterward.

<?php
 // Create an empty array
 $comlistall = array();

 foreach ( $comments as $comment ){
   $comlistall[] = $comment; // <= Populate the array with the comments in the loop
 };

 $rtitlez = get_the_title();
 $rimgz = get_the_post_thumbnail_url(get_the_ID(), 'full');
 $rvaluez = get_post_meta($post->ID, 'comments_rating_value', true);
 $rcountz = get_post_meta($post->ID, 'comments_rating_count', true);

 $result = [
  "@context" => "https://schema.org/",
  "@type" => "Organization",
  "name" => $rtitlez,
  "image" => $rimgz,
  "aggregateRating" => [
        "@type" => "AggregateRating",
        "ratingValue" => $rvaluez,
        "bestRating" => "5",
        "worstRating" => "1",
        "ratingCount" => $rcountz
      ],
   "comlistall" => $comlistall, // insert the comment array in your $results array
  ];
?>
<script type="application/ld+json"><?php echo json_encode($result); ?></script>

Update

If you only want certain attributes from the comments, simply create your own array with the attributes you want. In this example $comment is an array with the attributes. Modify to your needs.

// ...
foreach ( $comments as $comment ){
  $c = [
    "@type" => "Review",
    "headline" => $comment["headline"],
    "reviewbody" => $comment["reviewbody"],
    "datePublished" => $comment["datePublished"],
    "itemReviewed" => [
       "@type" => "Organization",
       "name" => $comment["name"],
    ],
    "author " => [
       "@type" => "Person",
       "name" => $comment["authorname"],
    ],  
  ];

  $comlistall[] = $c;
};
// ...
3
  • Your answer works. But now I'm having a problem with the output. comment or get_comments return everything on the comment. I need the desired output like the one I made in $comlist.
    – Bikram
    Feb 6, 2020 at 16:21
  • 1
    @Bikram I have updated the answer.
    – Tobias
    Feb 8, 2020 at 7:54
  • Hi, everything works now. But still, there is something extra added I think. I couldn't figure out the issue. Have a look - search.google.com/structured-data/testing-tool/u/0/…
    – Bikram
    Feb 8, 2020 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.