Skip to content

Instantly share code, notes, and snippets.

@danielpowney
Last active August 29, 2015 14:13
Show Gist options
  • Save danielpowney/7bf1df768ca655ef562d to your computer and use it in GitHub Desktop.
Save danielpowney/7bf1df768ca655ef562d to your computer and use it in GitHub Desktop.
<?php
/**
* Template Name: My Test Page Pro
*
* This page template is just an example of how you can use the API to display the rating results
* and the rating form using Multi Rating Pro. Twitter Bootstrap 3 scaffolding has used for the grid.
*/
get_header(); ?>
</header>
<section id="main">
<div class="container">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class("row"); ?>>
<div class="col-lg-12 col-sm-12 col-12">
<h1>
<?php
// ***
// ensure auto placement is not used. i.e. do not show for page
the_title();
?>
<?php
// ***
// get the rating form id, you can use the general settings and also check the post meta. If a
// rating form is not specified in post meta, use default settings
$rating_form_id = get_post_meta( $post->ID, MRP_Multi_Rating::RATING_FORM_ID_POST_META, true );
if ( $rating_form_id == '' ) {
$general_settings = (array) get_option( MRP_Multi_Rating::GENERAL_SETTINGS );
$rating_form_id = $general_settings[ MRP_Multi_Rating::DEFAULT_RATING_FORM_OPTION ];
}
// ***
// the rating results can be quickly retrieved using the API. The rating results
// are not calculated every time they're displayed - they are "cached" in the WP
// postmeta table. It is recommended to use the API instead of calling the get_post_meta()
// function as the APU does a check if the rating results need to be recalculated or not
$rating_result = MRP_Multi_Rating_API::get_rating_result( get_the_ID(), $rating_form_id );
// ***
// get what we need... we're using score results in this example. Adjusted ratings take into
// consideration any custom weights used for each rating item. You can also use "score_result".
$adjusted_score_result = $rating_result['adjusted_score_result'];
$total_max_option_value = $rating_result['total_max_option_value'];
$count_entries = $rating_result['count'];
if ( $count_entries > 0 ) {
$count_text = $count_entries == 1 ? __( 'vote' , 'my_text_domain' ) : __( 'votes', 'my_text_domain' );
// ***
// The rating result will be after the title. Lets display the result how we want e.g. Rated 10/15 (23 votes)
?>
<span class="rating-result mrp-filter rating-result-<?php echo $rating_form_id; ?>-<?php echo get_the_ID(); ?>">
<span class="score-result" style="font-size: 65% !important; vertical-align: top;"><?php echo __( 'Rated ', 'my_text_domain' ) . $adjusted_score_result . '/' . $total_max_option_value; ?></span>
<span class="count" style="font-size: 65% !important; vertical-align: top;">(<?php echo $count_entries . ' ' . $count_text; ?>)</span>
</span>
<?php
} else {
// ***
// Show the no rating results message. You can use the custom text settings if you like.
$custom_text_settings = get_option( MRP_Multi_Rating::CUSTOM_TEXT_SETTINGS );
$no_rating_results_text = $custom_text_settings[MRP_Multi_Rating::NO_RATING_RESULTS_TEXT_OPTION];
echo '<p class="no-rating-results">' . $no_rating_results_text . '</p>';
}
?>
</h1>
<?php the_content(); ?>
<?php
// ***
// Lets display the rating form here
MRP_Multi_Rating_API::display_rating_form( array(
'post_id' => get_the_ID(),
'rating_form_id' => $rating_form_id,
'echo' => true,
'submit_button_text' => __( 'Submit', 'my_text_domain' )
// there a heap of other options you can use
) );
?>
<?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
?>
</div>
</div>
<?php endwhile; ?>
</div>
</section>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment