Showing Content of Top Voted Post
-
Hi I would like to show the content of top voted Posts below the hyper link and vote counts. Is this possible?
Thanks,
O Dot
Viewing 2 replies - 1 through 2 (of 2 total)
-
Hey there,
Yes it’s posible but you’ll have to code a little.
You need to override the thumbs_rating_top_func() function and print whatever you want in it.
Try pasting this into your functions.php
function thumbs_rating_top_func( $atts ) { $return = ''; // Parameters accepted extract( shortcode_atts( array( 'type' => 'positive', 'posts_per_page' => 5, 'category' => '', 'show_votes' => 'yes', 'post_type' => 'any', 'show_both' => 'no' ), $atts ) ); // Check wich meta_key the user wants if( $type == 'positive' ){ $meta_key = '_thumbs_rating_up'; $other_meta_key = '_thumbs_rating_down'; $sign = "+"; $other_sign = "-"; } else{ $meta_key = '_thumbs_rating_down'; $other_meta_key = '_thumbs_rating_up'; $sign = "-"; $other_sign = "+"; } // Build up the args array $args = array ( 'post_type' => $post_type, 'post_status' => 'publish', 'cat' => $category, 'pagination' => false, 'posts_per_page' => $posts_per_page, 'cache_results' => true, 'meta_key' => $meta_key, 'orderby' => 'meta_value' ); // Get the posts $thumbs_ratings_top_query = new WP_Query($args); // Build the post list if($thumbs_ratings_top_query->have_posts()) : $return .= '<ol class="thumbs-rating-top-list">'; while($thumbs_ratings_top_query->have_posts()){ $thumbs_ratings_top_query->the_post(); $return .= '<li>'; $return .= get_the_content(); $return .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a>'; if( $show_votes == "yes" ){ // Get the votes $meta_values = get_post_meta(get_the_ID(), $meta_key); // Add the votes to the HTML $return .= ' (' . $sign; if( sizeof($meta_values) > 0){ $return .= $meta_values[0]; }else{ $return .= "0"; } // Show the other votes if needed if( $show_both == 'yes' ){ $other_meta_values = get_post_meta(get_the_ID(), $other_meta_key); $return .= " " . $other_sign; if( sizeof($other_meta_values) > 0){ $return .= $other_meta_values[0]; }else{ $return .= "0"; } } $return .= ')'; } } $return .= '</li>'; $return .= '</ol>'; // Reset the post data or the sky will fall wp_reset_postdata(); endif; return $return; }
Hi there,
I just noticed that my previous comment is wrong, the code will break the theme.
So here’s the code that actually works:
function my_thumbs_rating_top_func( $atts ) { $return = ''; // Parameters accepted extract( shortcode_atts( array( 'type' => 'positive', 'posts_per_page' => 5, 'category' => '', 'show_votes' => 'yes', 'post_type' => 'any', 'show_both' => 'no' ), $atts ) ); // Check wich meta_key the user wants if( $type == 'positive' ){ $meta_key = '_thumbs_rating_up'; $other_meta_key = '_thumbs_rating_down'; $sign = "+"; $other_sign = "-"; } else{ $meta_key = '_thumbs_rating_down'; $other_meta_key = '_thumbs_rating_up'; $sign = "-"; $other_sign = "+"; } // Build up the args array $args = array ( 'post_type' => $post_type, 'post_status' => 'publish', 'cat' => $category, 'pagination' => false, 'posts_per_page' => $posts_per_page, 'cache_results' => true, 'meta_key' => $meta_key, 'orderby' => 'meta_value' ); // Get the posts $thumbs_ratings_top_query = new WP_Query($args); // Build the post list if($thumbs_ratings_top_query->have_posts()) : $return .= '<ol class="thumbs-rating-top-list">'; while($thumbs_ratings_top_query->have_posts()){ $thumbs_ratings_top_query->the_post(); $return .= '<li>'; $return .= get_the_content(); $return .= '<a href="' . get_permalink() . '">' . get_the_title() . ' 123</a>'; if( $show_votes == "yes" ){ // Get the votes $meta_values = get_post_meta(get_the_ID(), $meta_key); // Add the votes to the HTML $return .= ' (' . $sign; if( sizeof($meta_values) > 0){ $return .= $meta_values[0]; }else{ $return .= "0"; } // Show the other votes if needed if( $show_both == 'yes' ){ $other_meta_values = get_post_meta(get_the_ID(), $other_meta_key); $return .= " " . $other_sign; if( sizeof($other_meta_values) > 0){ $return .= $other_meta_values[0]; }else{ $return .= "0"; } } $return .= ')'; } } $return .= '</li>'; $return .= '</ol>'; // Reset the post data or the sky will fall wp_reset_postdata(); endif; return $return; } add_shortcode( 'my_thumbs_rating_top', 'my_thumbs_rating_top_func' );
What we’re doing here is create a new shortcode. To use it you’d have to use it like this:
[my_thumbs_rating_top type="positive" posts_per_page="10" post_type="post" show_votes="no"]
As you can see the shortcode is now called: my_thumbs_rating_top
Sorry about that, enjoy ??
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Showing Content of Top Voted Post’ is closed to new replies.