WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] if Statement yes or no (6 posts)

  1. Micbow
    Member
    Posted 2 years ago #

    Hi I'm try to write an If statement for a custom field.

    The custom field is called "multiple_listings" and it have 2 values that can be selected from a drop down menu, "Yes" or "No"

    If "yes" is selected I want some HTML to appear.
    If "no" is selected I don't want anything to appear.

    So far I think the php needs to look something like this . . .

    <?php
    if ( get_post_meta($post_id, 'multiple_listings' = 'Yes') { ?>
    HTML I want to appear
    <?php } ?>

    I've tried several variations but I cannot get it to distinguish between the "yes" and the "no"

    Can someone provide me with the correct code please.

    Thanks in advance.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/get_post_meta

    the comparison operator is ==

    <?php
    if ( get_post_meta($post_id, 'multiple_listings', true) == 'Yes') { ?>
    HTML I want to appear
    <?php } ?>
  3. Micbow
    Member
    Posted 2 years ago #

    Hi Thanks
    Unfortunately that didn't work.

    Also tried replacing "post_meta" with "posts" and "custom"

    tried removing ",true" and "== 'Yes'"

    Any more ideas?

  4. Micbow
    Member
    Posted 2 years ago #

    I added a ";" as per below after the 'Yes');
    but it showed the content/HTML for both the "Yes" and the "No"

    <?php
    if ( get_post_meta($post_id, 'multiple_listings', true) == 'Yes'); { ?>
    HTML I want to appear
    <?php } ?>

  5. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try and replace the $post_id with $post->ID:

    if ( get_post_meta($post->ID, 'multiple_listings', true) == 'Yes') { ?>

    and check the spelling/capitalisation of 'Yes'

    what do you get when you output the post meta;

    example:

    <?php echo get_post_meta( $post->ID, 'multiple_listings', true ); ?>

  6. Micbow
    Member
    Posted 2 years ago #

    Excellent!
    That fixed it.
    Thank you very much for your help. Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic