WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display text if certain custom field text exists? (4 posts)

  1. greencode
    Member
    Posted 4 years ago #

    I know how to display a custom field if it simply exists:

    <?php
    $key = 'my-custom-field';
    $themeta = get_post_meta($post->ID, $key, TRUE);
    if($themeta != '') {
    echo 'Display this text if the custom field has been filled in';
    }
    ?>

    But is there any way to display some different text if that field has been filled in with certain text i.e. If the word Apple is inserted into the custom field I would like Fruit to be displayed on the web page and NOT the word Apple.

    I'm sure there must be a way to question whether a certain word exists in the custom field but I really don't know how!

    Any help would be greatly appreciated.

  2. Taylor Dewey
    Member
    Posted 4 years ago #

    After you've got the contents of your custom field, $themeta, you can set-up whatever test case you'd like. For example,

    if ($themeta == 'apple'){
    echo 'fruit';
    } else if ($themeta == 'monkey') {
    echo 'not fruit';
    } else {
    echo 'Not sure if this is fruit...';
    }
  3. greencode
    Member
    Posted 4 years ago #

    Thanks so much for your help with this - very much appreciated.

  4. Taylor Dewey
    Member
    Posted 4 years ago #

    Glad it helped.

Topic Closed

This topic has been closed to new replies.

About this Topic