I'm trying to incorporate "Thumbs Up" functionality into my wordpress website. The website already serves the buttons and people can rate using the buttons on each video page, but I want to be able to pull the posts with the highest thumbs up counts and display it on the front page.
Right now on the front page, there is a section that pulls in all related videos from the featured category. I would like to convert that section into thumbs up leaders from highest to lowest instead of the featured category. Also, I would like to display the number of thumbs up the post has instead of the date the post was added.
The code for the featured section on the front page is given below.
<? if ($svp_featured_videos_enabled == "true") { ?><? if ($svp_featured_post) { ?>
<div style="margin-top:24px;margin-bottom:8px;" class="maintitle_noborder maintitle_noborder_red"><span>Featured</span> Videos</div>
<div id="videos"><?php $recent = new WP_Query("cat=".$svp_featured_post."&showposts=".$svp_featured_index_posts."&orderby=date"); while($recent->have_posts()) : $recent->the_post();?>
<div class="posts_block">
<div class="posts_block_thumb"><a title="<?php the_title_attribute(); ?>" href="<?php the_permalink() ?>">
<?php $thumb = getcustomfield('videoswiper-embed-thumb',get_the_ID()); if(!empty($thumb)) { if(!strstr($thumb,"http://")){ $thumb = get_bloginfo('url').$thumb; } ?>
<img class="posts_block_thumbnail" src="<?php echo $thumb; ?>" alt="<?php the_title_attribute(); ?>" />
<?php }else { ?><img class="posts_block_thumbnail" src="<?php bloginfo('template_url'); ?>/images/no_image.gif" alt="<?php the_title_attribute(); ?>" /><?php } ?></a>
<?php $vid_time = getcustomfield('duration',get_the_ID()); ?><?php if ($vid_time[0]=="") { ?><!--show nothing--><?php } else { ?>
<div class="videos_bg_thumb_overlay"><?php echo $vid_time; ?></div><?php } ?>
</div>
<div class="posts_block_details">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo get_limited_string($post->post_title,28); ?></a>
</div>
<div class="posts_block_views_red">Added: <?php echo time_ago(); ?></div>
<div class="posts_block_views_red">Total: <?php echo getPostViews(get_the_ID()); ?></div>
</div>
<?php endwhile; ?>
</div><? } ?><? } ?>
Thumbs Up for wordpress is explained at:
http://wpfan.com/how-to-install-thumbsup-in-wordpress/
There is a section there under the heading "Get posts by rating" that explains how to do that.
Thanks.
Arnav Joy answers:
define this function in functions.php
function get_thumbs_posts($orderby='votes_balance', $limit='10', $sort='DESC', $type='post') {
global $wpdb;
$limit = $limit;
if(!empty($limit)){
$limit = 'LIMIT '.$limit;
}
$results = $wpdb->get_results("SELECT DISTINCT * FROM $wpdb->posts INNER JOIN (SELECT *, SUBSTRING(name, 6) as 'post_ID', votes_up - votes_down AS votes_balance, votes_up + votes_down AS votes_total FROM thumbsup_items) AS thumbsup ON $wpdb->posts.ID = thumbsup.post_ID WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = '$type' AND $wpdb->posts.post_password = '' ORDER BY $orderby $sort $limit");
return $results;
}
and call it as
<?php
$thumblist = get_thumbs_posts('votes_balance', '10');
foreach ($thumblist as $post) :
$url = get_permalink( $post->ID );
?>
<li><a href="<?php echo $url ?>"><?php echo $post->post_title ?></a></li>;
<?php endforeach; ?>
leet me know if any results comes out
Karlo Rihoo comments:
Hi,
I've already defined the function in functions.php but I would like to incorporate the second part into the code I provided.
Unfortunately, I don't have a test site setup for this. I'd like someone to modify the code above and incorporate it into the featured section before I make changes.
I guess since you said that, does it not look like it would work correctly, or is this not the way you would recommend?
Thanks.
Karlo Rihoo comments:
Actually,
Since you said that, I just tried it and it returned the links to the posts.
Any suggestions?
Arnav Joy comments:
ok please try this
i do know what your "time_ago" funtion does but i will modify it , once all the things is working
<?php
$thumblist = get_thumbs_posts('votes_balance', '10');
?>
<? if (!empty($thumblist) ) { ?>
<div style="margin-top:24px;margin-bottom:8px;" class="maintitle_noborder maintitle_noborder_red"><span>Featured</span> Videos</div>
<div id="videos">
<?php foreach ($thumblist as $post) :?>
<?php
$title = get_the_title( $post->ID );
$url = get_permalink( $post->ID );
?>
<div class="posts_block">
<div class="posts_block_thumb"><a title="<?php echo $title; ?>" href="<?php echo $url; ?>">
<?php $thumb = getcustomfield('videoswiper-embed-thumb',$post->ID); if(!empty($thumb)) { if(!strstr($thumb,"http://")){ $thumb = get_bloginfo('url').$thumb; } ?>
<img class="posts_block_thumbnail" src="<?php echo $thumb; ?>" alt="<?php echo $title; ?>" />
<?php }else { ?><img class="posts_block_thumbnail" src="<?php bloginfo('template_url'); ?>/images/no_image.gif" alt="<?php echo $title; ?>" /><?php } ?></a>
<?php $vid_time = getcustomfield('duration',$post->ID); ?><?php if ($vid_time[0]=="") { ?><!--show nothing--><?php } else { ?>
<div class="videos_bg_thumb_overlay"><?php echo $vid_time; ?></div><?php } ?>
</div>
<div class="posts_block_details">
<a href="<?php echo $url; ?>" rel="bookmark" title="<?php echo $title; ?>"><?php echo get_limited_string($title,28); ?></a>
</div>
<div class="posts_block_views_red">Added: <?php echo time_ago(); ?></div>
<div class="posts_block_views_red">Total: <?php echo getPostViews($post->ID); ?></div>
</div>
<?php endforeach; ?>
</div><? } ?>
Karlo Rihoo comments:
That works like a charm. Does it not make sense to include this anymore (at the top)?
if ($svp_featured_videos_enabled == "true") { ?><? if ($svp_featured_post)
First part is asking if featured videos is enabled for the front page; not sure what the second part is asking?
Also, how do we get the # of thumbs up for each post. I want to replace that with the time_ago.
Thanks.
Arnav Joy comments:
here is the full code with above condition
<?php
if ($svp_featured_videos_enabled == "true") {
if ($svp_featured_post) {
$thumblist = get_thumbs_posts('votes_balance', '10');
?>
<? if (!empty($thumblist) ) { ?>
<div style="margin-top:24px;margin-bottom:8px;" class="maintitle_noborder maintitle_noborder_red"><span>Featured</span> Videos</div>
<div id="videos">
<?php foreach ($thumblist as $post) :?>
<?php
$title = get_the_title( $post->ID );
$url = get_permalink( $post->ID );
?>
<div class="posts_block">
<div class="posts_block_thumb"><a title="<?php echo $title; ?>" href="<?php echo $url; ?>">
<?php $thumb = getcustomfield('videoswiper-embed-thumb',$post->ID); if(!empty($thumb)) { if(!strstr($thumb,"http://")){ $thumb = get_bloginfo('url').$thumb; } ?>
<img class="posts_block_thumbnail" src="<?php echo $thumb; ?>" alt="<?php echo $title; ?>" />
<?php }else { ?><img class="posts_block_thumbnail" src="<?php bloginfo('template_url'); ?>/images/no_image.gif" alt="<?php echo $title; ?>" /><?php } ?></a>
<?php $vid_time = getcustomfield('duration',$post->ID); ?><?php if ($vid_time[0]=="") { ?><!--show nothing--><?php } else { ?>
<div class="videos_bg_thumb_overlay"><?php echo $vid_time; ?></div><?php } ?>
</div>
<div class="posts_block_details">
<a href="<?php echo $url; ?>" rel="bookmark" title="<?php echo $title; ?>"><?php echo get_limited_string($title,28); ?></a>
</div>
<div class="posts_block_views_red">Added: <?php echo time_ago(); ?></div>
<div class="posts_block_views_red">Total: <?php echo getPostViews($post->ID); ?></div>
</div>
<?php endforeach; ?>
</div><? } ?>
<?php } } ?>
Karlo Rihoo comments:
Hi,
That worked out like a charm. Also,
the code you gave me to display the thumbs up
also worked out like a charm.
Thanks.