Skip Navigation

[Resolved] If else statement isn't picking up a field value. Just need some syntax help?

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by michaelS-53 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1229507

I am trying to make it so that when a particular field's value is filled with a specific option, it echos out a specific bit of code. My code is:

<?php
$score = types_render_field('overall-score',array('raw'=>true));
if ( $score == 'Positive') {
echo '<div class="score-positive"><i class="fas fa-plus-circle">Asses</div>';
}
else {
echo 'Failed';
}
?>

I'm not sure what I've got wrong here.

#1229524

Hello, sorry there seems to have been a problem with the chat system. One thing to remember here is that types_render_field may produce extra markup, depending on the type of field. If you want the raw value from the database, you can use get_post_meta instead. In this syntax, you must also use the wpcf- prefix for the field, like this:

$post_id = 12345;
$score = get_post_meta( $post_id, 'wpcf-overall-score', true);
if ( $score == 'Positive') {
echo '<div class="score-positive"><i class="fas fa-plus-circle">Asses</div>';
}
else {
echo 'Failed';
}
#1229551

Hey Christian,

Thanks for the response. Unfortunately it's still just returning the else. I did try modifying it slightly since I'm not sure how to use the post_id here properly:

<?php
$score = get_post_meta( $post->ID, 'wpcf-overall-score', true);
if ( $score == 'positive') {
echo '<div class="score-positive"><i class="fas fa-plus-circle">Sum</div>';
}
else {
echo 'Failed';
}
?>

But even unmodified it still posted as "Failed". Any ideas?

#1229556

My issue is resolved now. Thank you!