Greetings, all.

I've been trying to figure out how to display a fivestar voting result in teaser view ... But the result only, so that people can't vote without viewing the full node. But I'm not having much luck.

I can create views which have this functionality (displaying only the result), so I'm sure a function must exist ... but how would I go about adding this to a node teaser?

Thanks in advance!

-jthorson

Comments

spooky69’s picture

Subscribing

quicksketch’s picture

The best way to do this is by defining some new variables in your template.php file and then using them in your node.tpl.php files.

  $vars['current_rating'] = votingapi_get_voting_result('node', $vars['node']->nid, 'percent', 'vote', 'average');
  $vars['vote_display'] = theme('fivestar_static', $vars['current_rating']->value);

If you're not familiar with using template.php, you could use this slightly more hackish approach, either using contemplate module or directly in your node.tpl.php files.

  $current_rating = votingapi_get_voting_result('node', $node->nid, 'percent', 'vote', 'average');
  print theme('fivestar_static', _rating->value);
jthorson’s picture

Status: Active » Closed (fixed)

Almost perfect ... the second option should be written as:

print theme('fivestar_static', $current_rating->value);

But you gave me enough that I was able to piece it together from there ... the 'fivestar_static' function was the piece I was missing.

Many thanks!

Setting the ticket status to Closed.

seanburlington’s picture

Version: 5.x-1.7 » 6.x-1.19

I came across this issue looking for the solution in Drupal 6

In case anyone else needs it - put the following in hook_preprocess_node()

$criteria = array('content_id' => $vars['nid'], 'value_type' => 'percent');      
$current_rating = votingapi_select_single_result_value($criteria ); 
$vars['vote_display'] = theme('fivestar_static', $current_rating); 

(assuming your votes are for nodes)

halloffame’s picture

How about if I want to print the average in numeric value?

halloffame’s picture

Status: Closed (fixed) » Active
jthorson’s picture

Status: Active » Closed (fixed)

evilgenius,

If you have a new question, please open it as a new issue. (You could, however, place a link to this issue in your new issue, to help provide context.)

As for your question, output the $current_rating value directly (without wrapping it in the theme() function).