首页 > 解决方案 > WordPress 发布评级显示位置更改

问题描述

我是一名新开发人员。此代码使用表单插件来显示平均评分及其运行良好。但其显示评级高于 WP 内容。我想在我的博客页面和单页的几个特定位置显示评级。我怎样才能做到这一点?

add_filter( 'the_content', 'ci_comment_rating_display_average_rating' );
function ci_comment_rating_display_average_rating(  ) {
    global $post;
    if ( false === ci_comment_rating_get_average_ratings( $post->ID ) ) {
        return $content;
    }
    $stars   = '';
    $average = ci_comment_rating_get_average_ratings( $post->ID );
    for ( $i = 1; $i <= $average + 1; $i++ ) {
        $width = intval( $i - $average > 0 ? 20 - ( ( $i - $average ) * 20 ) : 20 );
        if ( 0 === $width ) {
            continue;
        }
        $stars .= '<span style="overflow:hidden; width:' . $width . 'px" class="dashicons dashicons-star-filled"></span>';

        if ( $i - $average > 0 ) {
            $stars .= '<span style="overflow:hidden; position:relative; left:-' . $width .'px;" class="dashicons dashicons-star-empty"></span>';
        }
    }
    $custom_content  = '<p class="average-rating">This post\'s average rating is:-- ' . $average .' ' . $stars .'</p>';
    $custom_content .= $content;
    return $custom_content;
}

标签: phpwordpressfunctionrating

解决方案


推荐阅读