如何把这个结果放在那个结果里面呢?

时间:2020-02-06 作者:Bikram

我想把我的评论结果放入另一个结果中。

我想要这个结果

        $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 ;

                };
把这个结果放进去。

    <?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>
我该怎么做?

1 个回复
SO网友:Tobias

提前运行循环并填充数组,然后在$result 后来

<?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

如果只需要注释中的某些属性,只需使用所需的属性创建自己的数组即可。在此示例中$comment 是具有属性的数组。根据您的需要进行修改。

// ...
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;
};
// ...

相关推荐