检索所需结果的注释属性的正确方法

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

我正在使用此代码,但无法使其100%正常工作。注释返回与之相关的所有内容,但我只想检索具有所需属性的注释。

<?php

    $args = array(
                    \'post_id\'   =>$post->ID,
                    \'status\'  => \'approve\',
                    \'type\'  => \'wp_review_comment\',
                );
     $comments = get_comments( $args );

    // Create an empty array
    $comlistall = array();

    foreach ( $comments as $comment ){

          $c = [ "review" => [
            "@type" => "Review",
            "headline" => (get_comment_meta($comment->comment_ID, WP_REVIEW_COMMENT_TITLE_METAKEY, true)),
            "reviewbody" => ($comment->comment_content),
            "datePublished" => (get_comment_date( \'d\\/m\\/Y\' )),
            "itemReviewed" => [
                "@type" => "Organization",
                "name" => (get_the_title()),
                ],
            "author " => [
                "@type" => "Person",
                "name" => ($comment->comment_author),
                ],
            "reviewRating"=> [
                "@type" => "Rating",
                "ratingValue" => (get_comment_meta($comment->comment_ID, WP_REVIEW_COMMENT_RATING_METAKEY, true)),
                ],
             "publisher" => [
                 "@type" => "Organization",
                 "name" => "Sitejury",
                 "sameAs" => "https://sitejury.com",
                ],
            ]
        ];  
        $comlistall[] = $c;
    };

$result = [
  "@context" => "https://schema.org/",
  "@type" => "Organization",
  "name" => (get_the_title()),
  "image" => (get_the_post_thumbnail_url(get_the_ID(), \'full\')),
  "aggregateRating" => [
        "@type" => "AggregateRating",
        "bestRating" => "5",
        "worstRating" => "1",
        "ratingValue" => (get_post_meta($post->ID, \'wp_review_comments_rating_value\', true)),
        "ratingCount" => (get_post_meta($post->ID, \'wp_review_comments_rating_count\', true)),
  ],
    $comlistall,
] ;?>                       

<script type="application/ld+json"><?php echo json_encode($result); ?></script>
目前,我的问题是,现在的结果在"0" = [{ 属性我正在努力摆脱"0" = [{ 属性

结果url:https://sitejury.com/review/zzzzzz/

检查结构化数据:https://search.google.com/structured-data/testing-tool/u/0/#url=https%3A%2F%2Fwww.sitejury.com%2Freview%2Fzzzzzz%2F

回拨问题:How to put this result inside that result.

我想要的结果:https://search.google.com/structured-data/testing-tool/u/0/#url=https%3A%2F%2Fwww.sitejabber.com%2Freviews%2Fbillmelater.com

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

首先get_comment_date( \'d\\/m\\/Y\' ) 在我的案例中导致了一个PHP通知,所以我将其更改为get_comment_date( \'d\\/m\\/Y\', $comment ) (即,我通过$comment 对象)。

现在代码的问题是$result 应该有一个review 值为审阅数组的项(审阅对象):

$result = [
  "@context" => "https://schema.org/",
  "@type" => "Organization",
  "name" => (get_the_title()),
  "image" => (get_the_post_thumbnail_url(get_the_ID(), \'full\')),
  "aggregateRating" => [
    ...
  ],
  \'review\' => $comlistall, // here, a "review" item must exists
/* Or something like so:
  \'review\' => array(
    array( "@type" => "Review", "headline" => ..., "reviewbody" => ..., ... ),
    array( "@type" => "Review", "headline" => ..., "reviewbody" => ..., ... ),
    ...
  ),
*/
];
其次,你的$comlistall 应该生成一个数组,当它以JSON格式表示时,它看起来像第一个here (参见“更改的文本”)。

因此,要解决此问题:

如上所述$result, 使用\'review\' => $comlistall.

更改此选项:

$c = [ "review" => [
  "@type" => "Review",
  "headline" => (get_comment_meta($comment->comment_ID, WP_REVIEW_COMMENT_TITLE_METAKEY, true)),
  "reviewbody" => ($comment->comment_content),
  ...
] ];
$comlistall[] = $c;
对此(但您可以将审查数据/对象分配给变量$c):

$comlistall[] = [
  "@type" => "Review",
  "headline" => (get_comment_meta($comment->comment_ID, WP_REVIEW_COMMENT_TITLE_METAKEY, true)),
  "reviewbody" => ($comment->comment_content),
  ...
];

相关推荐

Comments.php保留评论日期/时间,但删除日期/时间的#超级链接

我在谷歌上搜索了这个问题,似乎找不到解决方案。。。在评论中,我试图从评论日期/时间中删除超链接,当您将鼠标悬停在评论日期上方时,它会将超链接(示例/#comment-210)链接到以下评论。。。我可以在函数中输入什么。php删除链接,我想保留日期/时间文本。。