admin管理员组

文章数量:1391947

I'm playing with this code but can't get it to work 100%. Comments return everything associated with it, but I want to retrieve the comments with only the desired attributes.

<?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" => "",
                ],
            ]
        ];  
        $comlistall[] = $c;
    };

$result = [
  "@context" => "/",
  "@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>

Currently, my problem is that now the results are rapped in a "0" = [{ attribute. I'm trying to get rid of the "0" = [{ attribute.

result url: /

Checking structured data: .sitejury%2Freview%2Fzzzzzz%2F

Callback question: How to put this result inside that result.

Result I want: .sitejabber%2Freviews%2Fbillmelater

I'm playing with this code but can't get it to work 100%. Comments return everything associated with it, but I want to retrieve the comments with only the desired attributes.

<?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",
                ],
            ]
        ];  
        $comlistall[] = $c;
    };

$result = [
  "@context" => "https://schema/",
  "@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>

Currently, my problem is that now the results are rapped in a "0" = [{ attribute. I'm trying to get rid of the "0" = [{ attribute.

result url: https://sitejury/review/zzzzzz/

Checking structured data: https://search.google/structured-data/testing-tool/u/0/#url=https%3A%2F%2Fwww.sitejury%2Freview%2Fzzzzzz%2F

Callback question: How to put this result inside that result.

Result I want: https://search.google/structured-data/testing-tool/u/0/#url=https%3A%2F%2Fwww.sitejabber%2Freviews%2Fbillmelater

Share Improve this question edited Feb 16, 2020 at 17:44 Bikram asked Feb 14, 2020 at 17:21 BikramBikram 3386 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1 +50

First off, the get_comment_date( 'd\/m\/Y' ) resulted in a PHP notice in my case, so I changed that to get_comment_date( 'd\/m\/Y', $comment ) (i.e. I pass the $comment object to the function).

Now the issue with your code is that the $result should have a review item with the value being an array of reviews (review objects):

$result = [
  "@context" => "https://schema/",
  "@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" => ..., ... ),
    ...
  ),
*/
];

Secondly, your $comlistall should produce an array which when it's represented in JSON format, looks like the first one here (see the "Changed Text").

So to fix the issue:

  1. As I mentioned above, in the $result, use 'review' => $comlistall.

  2. Change this:

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

    to this (but you may assign the review data/object to the variable $c):

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

本文标签: Correct way to retrieve comment attributes for desired results