• Trying to fix this and I know it’s easy, but cannot figure it out for the life of me. I just need to check if my metadata checkbox is checked. Basically I have a custom post type called products that works great in my catalog loop. However, I have a homepage with a small gallery that only displays a select few. This is where my gallery checkbox comes into play.

    However, I cannot get an if statement to work correctly. I don’t get why my logic isn’t working. I just get a bunch of ‘Don’t show in gallery’ lines even though the checkbox is checked and saved/updated correctly. Basically, it’s saying they are all false and I have two of them checked and saved for testers. WEIRD!

    $gallery = get_post_meta($post->ID, 'gallery', true);
    
    if($gallery == true) {
     echo 'Show in gallery';
     } else {
    echo 'Don't show in gallery';
    }
Viewing 1 replies (of 1 total)
  • C W (VYSO)

    (@cyril-washbrook)

    The value of $gallery is not boolean. That is, it does not evaluate as true or false. You can see this by ticking a checkbox and simply echoing $gallery. You should see that its value is 1.

    You should be able to achieve the desired result simply by removing == true from the if statement. That is, use something like:

    if($gallery) :
       echo 'Show in gallery';
    else :
       echo 'Don't show in gallery';
    endif;

    Broadly speaking, if($variable) will return a positive result if the value is not false and not zero. A more precise explanation can be found in the PHP manual.

Viewing 1 replies (of 1 total)
  • The topic ‘if metdata is checked not working’ is closed to new replies.