最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - Show the "ratingValue" and "ratingCount" values ​of KK Star Ratings Plugin

programmeradmin1浏览0评论

I want to display the "ratingValue" and "ratingCount" values ​​generated by the KK Star Ratings plugin (Github).

I made a JSON-LD SoftwareAplication and here is the code I use in function.php:

add_action('wp_head', 'add_jsonld_head', 1);
function add_jsonld_head() {
    if ( is_single() ) {
        ?>
        <script type="application/ld+json">
            {
      "@context": "/",
      "@type": "SoftwareApplication",
      "@id": "<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>",
      "softwareVersion": "<?php the_field('versi_terbaru'); ?>",
      "operatingSystem": "Windows",
      "applicationCategory": "<?php the_field('kategori'); ?>",
      "dateModified": "<?php the_modified_time('c'); ?>",
      "name": "<?php the_title(); ?>",
      "aggregateRating": {
        "@type": "AggregateRating",
        "name": "<?php the_title(); ?>",
        "ratingValue": "",
        "ratingCount": "",
        "bestRating": "5",
      },
      "publisher": {
        "@type": "organization",
        "name": "<?php the_title(); ?>"
      },
      "offers": {
        "@type": "Offer",
        "price": "0",
        "priceCurrency": "IDR"
      },
      "image":{
        "@type": "ImageObject",
        "url": "<?php the_post_thumbnail_url(); ?>",
        "width": "100",
        "height": "100"
      }
    }
        </script>
        <?php
    }
} 

How do I get the ratingValue and ratingCount values ​​from KK Star Ratings?

I have played around with this plugins but sadly my coding skills are not that good so I couldn't figure out how to modify it. I would really appreciate any help I can get on this.

I want to display the "ratingValue" and "ratingCount" values ​​generated by the KK Star Ratings plugin (Github).

I made a JSON-LD SoftwareAplication and here is the code I use in function.php:

add_action('wp_head', 'add_jsonld_head', 1);
function add_jsonld_head() {
    if ( is_single() ) {
        ?>
        <script type="application/ld+json">
            {
      "@context": "http://schema/",
      "@type": "SoftwareApplication",
      "@id": "<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>",
      "softwareVersion": "<?php the_field('versi_terbaru'); ?>",
      "operatingSystem": "Windows",
      "applicationCategory": "<?php the_field('kategori'); ?>",
      "dateModified": "<?php the_modified_time('c'); ?>",
      "name": "<?php the_title(); ?>",
      "aggregateRating": {
        "@type": "AggregateRating",
        "name": "<?php the_title(); ?>",
        "ratingValue": "",
        "ratingCount": "",
        "bestRating": "5",
      },
      "publisher": {
        "@type": "organization",
        "name": "<?php the_title(); ?>"
      },
      "offers": {
        "@type": "Offer",
        "price": "0",
        "priceCurrency": "IDR"
      },
      "image":{
        "@type": "ImageObject",
        "url": "<?php the_post_thumbnail_url(); ?>",
        "width": "100",
        "height": "100"
      }
    }
        </script>
        <?php
    }
} 

How do I get the ratingValue and ratingCount values ​​from KK Star Ratings?

I have played around with this plugins but sadly my coding skills are not that good so I couldn't figure out how to modify it. I would really appreciate any help I can get on this.

Share Improve this question asked Jun 13, 2020 at 5:42 R.M. RezaR.M. Reza 1417 bronze badges 3
  • this plugin uses 'casts' and 'ratings' as meta key. do some test and figure out the rest. you have to use get_post_meta function – Sabbir Hasan Commented Jun 13, 2020 at 7:17
  • @SabbirHasan Can you help me? There is no other solution besides using this method, I am very confused. – R.M. Reza Commented Jun 13, 2020 at 7:53
  • I'll try to add some code example as answer. First I need to install the plugin and test my code so that I can guide you. Have patience please. – Sabbir Hasan Commented Jun 14, 2020 at 12:10
Add a comment  | 

2 Answers 2

Reset to default 1

Here is what I found that might help you. I did some test by myself and here is snippets for you to use. Add the following code to functions.php file:

//Getter methods

//this will return the base best score like 5 or 10.
function get_best_rating(){
    return max((int) get_option('kksr_stars'), 1);
}


//This will return the rating vote count
function get_rating_count($id){
    return count_filter(null, $id, null);
}

//This will return the rating score value
function get_rating_score($id){
    return score_filter(null, $best, $id, null);
}

//Helper Functions
function count_filter($count, $id, $slug){
    if ($slug) {
        return $count;
    }

    $count = (int) get_post_meta($id, '_kksr_casts', true);

    return max($count, 0);
}

function score_filter($score, $best, $id, $slug){
    if ($slug) {
        return $score;
    }

    $count = count_filter(null, $id, null);
    $counter = (float) get_post_meta($id, '_kksr_ratings', true);

    if (! $count) {
        return 0;
    }

    $score = $counter / $count / 5 * $best;
    $score = round($score, 1, PHP_ROUND_HALF_DOWN);

    return min(max($score, 0), $best);
}

Now you can call the getter function like this:

$best = get_best_rating();
$count = get_rating_count(get_the_id()); //You must provide post id as param
$score = get_rating_score(get_the_id()); //You must provide post id as param

Let me know if it helps you.

This is the code I used:

$best  = get_option('kksr_stars');
$score = get_post_meta(get_the_ID(), '_kksr_ratings', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_ratings', true)) : 0;
$votes = get_post_meta(get_the_ID(), '_kksr_casts', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_casts', true)) : 0;
$avg = $score && $votes ? round((float)(($score/$votes)*($best/5)), 1) : 0;

Call:

<?php echo $avg; ?>
<?php echo $votes; ?>    
发布评论

评论列表(0)

  1. 暂无评论