-1

How can I display certain image in a post if that post contains a specific custom value. For example, I have two posts, one has a custom field named top_post and its value is true. The other one doesn't have that value at all. I want the one with this value set to true to have image displayed (an image that shows it's a top post) and the one without the value to show nothing.

I know that the function would be something like "if top_post value is 'true' then display img(url)" but I don't know how to write that function as I'm just a PHP beginner.

Any help appreciated.

EDIT: I tried finding the function code on various coding blogs but didn't succeed... Also, I have no function coding experiences and that's why I was asking for a function code.

rlab
  • 449
  • 1
  • 7
  • 24

2 Answers2

1

You're on the right track, I think what you need to keep in mind is that you're using PHP to generate the HTML on the page. So if you want an image to show up if a certain condition is true, you only generate the HTML for that image if the condition is true:

if(top_post)
    echo "<img src='top_post_image.png'>"
Atiaxi
  • 1,637
  • 1
  • 13
  • 18
1

To expand on Atiaxi's answer, and perhaps make it a bit more readable for you as a beginner in php:

if ($top_post == true)
{
    echo "<img src='top_post_image.png'>";
} else {
    echo "<img src='blank.png'>"; //you could also leave this line out if you want 'nothing'
}
hyarion
  • 2,251
  • 2
  • 17
  • 28
  • How does it know that it has to search for `top_post` in a custom field? Asking that because I tried it and it doesn't work, it goes to `else` i.e. shows blank.png... – rlab Nov 15 '11 at 13:32
  • You havn't explained how you are getting your "top_post" value. E.g. are you reading it from a database? are you getting it from a form? – hyarion Nov 15 '11 at 13:39
  • As an example, if you are getting it from a form then you would do something like this: $top_post = $_POST['top_post']; – hyarion Nov 15 '11 at 13:41
  • I guess I can use `ID, true);} ?>` but I don't know how it can just check the value and not display it. This is from functions.php: `function get_custom_field_value($szKey,$postId, $bPrint = false) {global $post; $szValue = get_post_meta($postId, $szKey, true); if ( $bPrint == false ) return $szValue; else echo $szValue;}`, it's the one referenced with that code above. – rlab Nov 15 '11 at 13:43
  • ok, using that code you would do this: ID, true);} ?> This will put the value into $top_post of true/false. – hyarion Nov 15 '11 at 13:46
  • No, that just displays the value of `top_post` custom field. It doesn't check that it says "true", therefore doesn't display an image. Could you write the complete code that echos the image (top_post_image.png) if the value in `top_post` = "true"? – rlab Nov 15 '11 at 13:51
  • Sorry, what I meant was that you need to use the `$top_post=` part to get the value from the custom fields. Then after you've done that you need the code from my original post. Below is full code: (still learning stackoverflow so formatting might not be right) if ( function_exists('get_custom_field_value') ) { //set $top_post to true or false. $top_post = get_custom_field_value('top_post',$post->ID, true); } if ($top_post == true) { echo ""; } else { echo ""; //you could also leave this line out if you want 'nothing' } – hyarion Nov 15 '11 at 14:16
  • OK, here's a final code that works: `
    ID, 'top_post', true); if($top_post == 'true'): echo ""; else: echo ""?>
    `. Don't know why but I can't use wiggly brackets with `echo`, it gives me an error. Thanks for your help.
    – rlab Nov 15 '11 at 17:15