• I have created a custom post template and a plugin. The plugin extends the comments template, adding custom ratings. Now that I have these ratings, I am averaging them and want to display the average in the custom post template. In the plugin I make the calculations,

    add_filter( ‘comment_text’, ‘calc_avg’);
    function calc_avg( $post_id ) {
    // Calculate rating averages
    $args = array(
    ‘ID’ => $post_id,
    ‘status’ => ‘approve’,
    );
    $comments = get_comments( $args );
    foreach( $comments as $comment ) {
    $tot_stars += get_comment_meta( $comment->comment_ID, ‘rating’, true );
    }
    $no_of_comments = get_comments_number( $post_id );
    if ($no_of_comments > 0) {
    $avg_rating = round($tot_stars / $no_of_comments);
    }
    update_post_meta($post_id, ‘avg_comment_rating’, $avg_rating);
    echo $avg_rating;
    }
    I then try to display the $avg_rating in the custom post template using:

    $avg_rating = get_post_meta($post->ID, ‘avg_comment_rating’, true);
    echo get_post_meta($post->ID, ‘avg_comment_rating’, true);
    However, this does not yield a result. However, it does work when I echo the $avg_rating in the plugin. How can I call my $avg_rating variable in the custom post template?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Output a variable in a custom post template from a plugin’ is closed to new replies.