• Resolved jarvo1980

    (@jarvo1980)


    Hi All,

    I’m trying to test if 2 custom fields have info, it both have details, then display a message. My code below errors with

    unexpected ‘:’

    I thought I needed the : can someone please put me out my misery.

    <?php if (get_post_meta($post->ID, "Address", $single = true && get_post_meta($post->ID, "Address2", $single = true)  :
    	echo 'both present';
    endif; ?>

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Take the : out, you don’t need it.

    You are missing the final )’s in your calls to get_post_meta(). I suggest you use a proper editor which can highlight simple mistakes such as these.

    Also, you cannot check for equality with = as it is the assignment operator. Use == instead.

    On a side note, I hate the syntax with the :… just use { }. Much prettier ??

    Example for checking the custom fields:

    <?php
    $field_one = get_post_meta($post->ID, "Address", true );
    $field_two = get_post_meta($post->ID, "Address2", true );
    if( ( $field_one && '' != $field_one ) && ( $field_two && '' != $field_two ) ) :
    
    	echo 'Both fields are present and not empty strings';
    
    endif;
    ?>

    Thread Starter jarvo1980

    (@jarvo1980)

    Thanks all for your replies, Mark / t31os your code worked like a charm!
    Cheers

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘if and statement with custom fields’ is closed to new replies.