0

Ok, the code below works. I've crated a custom field for scores from Rotten Tomatoes that I can add to my Blu-Ray reviews. Based on its score (above 60 will get a "fresh" rating, below a "rotten") it'll display the appropriate image.

This works fine.

But...it also displays on every page; even those without scores assigned yet.

<?php
  global $wp_query;
  $postid = $wp_query->post->ID;

$result = ( get_post_meta($postid, 'ecpt_tomatometer', true));

if ($result >= 60) {
    echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
  } 
else {
    echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
  } 
?>

Now below is another snippet of code that works, yet I can't seem to intertwine the two. This (below) basically says "if there's something in the custom field, then display the code (above), otherwise don't display anything.

So I've got both parts of what I want to work, but I can't seem to get them to work together.

<?php
global $wp_query;
$postid = $wp_query->post->ID;
if( get_post_meta($postid, 'tomatometer', true)) 
{ ?>
This won't show up if there's nothing in the field.
<?php } 
           elseif( get_post_meta($postid, 'ecpt_tomatometer', true)) { 
?>
this will display all of the information I need it to.
<?php } ?>

Ideas?

2 Answers 2

1

Do the result comparison only if the result has a value (assuming null or false if no rating).

global $wp_query;
$postid = $wp_query->post->ID;
if($result = get_post_meta($postid, 'ecpt_tomatometer', true)){
if ($result >= 60) {
        echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
} 
else {
        echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

not sure of the possible values of $result but I would try something like:

<?php
  global $wp_query;
  $postid = $wp_query->post->ID;

$result = ( get_post_meta($postid, 'ecpt_tomatometer', true));
if (!($result===false) && intval($result) > 0)
{
if ($result >= 60) {
    echo '<img src="/images/misc/fresh.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
  } 
else {
    echo '<img src="/images/misc/rotten.png" width="102" height="25"> <span class=tomatometer>' . get_post_meta($postid, 'ecpt_tomatometer', true) . '%</span><br />';
  }
} 
?>

This should work.

2 Comments

This one didd the trick for me. I have no idea why, but it worked. Thank you.
This won't display a a rating of zero (if that's possible). And if you want to optimize it, you don't need to call get_post_meta the second time as you already have its value stored in $result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.