显示KK星级评级插件的“ratingValue”和“ratingCount”值​

时间:2020-06-13 作者:R.M. Reza

我想显示“ratingValue”和“ratingCount”值​​由生成KK Star Ratings plugin (Github).

我制作了一个JSON-LD软件应用程序,下面是我在函数中使用的代码。php:

add_action(\'wp_head\', \'add_jsonld_head\', 1);
function add_jsonld_head() {
    if ( is_single() ) {
        ?>
        <script type="application/ld+json">
            {
      "@context": "http://schema.org/",
      "@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
    }
} 
如何获取ratingValue和ratingCount值​​来自KK明星收视率?

我曾经尝试过这个插件,但遗憾的是,我的编码技术不是很好,所以我不知道如何修改它。如果能在这方面得到任何帮助,我将不胜感激。

2 个回复
最合适的回答,由SO网友:Sabbir Hasan 整理而成

这是我发现的可能对你有帮助的东西。我自己做了一些测试,这里有一些片段供您使用。将以下代码添加到函数中。php文件:

//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);
}
现在可以如下调用getter函数:

$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
如果对你有帮助,请告诉我。

SO网友:R.M. Reza

这是我使用的代码:

$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;
电话:

<?php echo $avg; ?>
<?php echo $votes; ?>    

相关推荐

阻止网络上的JSON访问

我的一个网站被一家安全公司扫描后,发现下面的内容需要屏蔽”http://domain.com/wp-includes/sodium_compat/composer.json“通过互联网访问。我尝试了几个插件,但都不起作用,有人知道如何阻止类似文件在浏览器上显示JSON吗。