• Resolved Linosa

    (@linosa)


    I’m using the types plugin for custom fields and I’ve would like to check if my custom field exist before posting it.

    I’ve found this code, but don’t know how to use it (my php skills aren’t that good).

    <?php
    
    //Check if custom field is empty
    if (!((get_post_meta($post->ID, 'wpcf-email', TRUE))=='')) {
    
    //Not empty
    
    echo get_post_meta($post->ID,'wpcf-email',TRUE);
    
    }
    ?>

    My code looks like this today:

    <div class="spotify">
        	<a href="<?php echo types_render_field("spotify-url", array("argument1"=>"value1","argument2"=>"value2","argument2"=>"value2"));
    		?>" target="_blank"><?php echo types_render_field("spotify-text", array("argument1"=>"value1","argument2"=>"value2","argument2"=>"value2"));
    		?></a>
        </div>

    And I would like something like:
    if spotify-url exist show
    xxx-my-code-xxx
    if not show
    nothing.

    Any suggestions?

Viewing 2 replies - 1 through 2 (of 2 total)
  • C W (VYSO)

    (@cyril-washbrook)

    <?php
    $spotify_url = get_post_meta(get_the_ID(), 'wpcf-spotify-url', true);
    if( !empty($spotify_url) ) { ?>
       <!-- Do stuff if there is a Spotify URL -->
    <?php } else { ?>
       <!-- Do different stuff if there isn't a Spotify URL -->
    <?php } ?>

    (Note: you may have noticed that I referred to wpcf-spotify-url at the start of the code snippet. wpcf- is a prefix that Types adds to all custom fields that it creates. When you use a generic WordPress function like get_post_meta, you have to add the wpcf- prefix. When you use a Types function like types_render_field, you leave it out.)

    Thread Starter Linosa

    (@linosa)

    It works perfect. Thank you very much!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to check if custom field from Types plugin exist’ is closed to new replies.