WordPress.org

Ready to get started?Download WordPress

Forums

Types - Custom Fields and Custom Post Types Management
[resolved] How-to? Check if Custom Field is Fi (5 posts)

  1. iamSeanGrant
    Member
    Posted 2 years ago #

    Hello, Great plugin, makes setting up custom post types and custom fields super easy. :)

    Now I want to interact with them. :)

    I know how to display a custom field, manually, without Views Plugin, but How can I check if a custom filed is filled in, and thus echo the field or not?

    I tried simple if isset like this:
    $prop_city_info = types_render_field("prop-city-info", array("raw"=>"true")); if (isset($prop_city_info))

    But it comes back TRUE even though there is no output when I echo the variable.
    This custom filed is a File type. And this custom field is empty for sure.

    I'm using a custom theme I created. it's very simple. WP 3.3.2 and Types 1.0.1
    I've created a custom_post_type of Properties
    I've created a Group of Custom Fields called Property Attributes
    The custom fields are; address, city info (PDF), marketing flyer (PDF), etc. mostly File types (PDFs) and a few TextFields (simple strings)

    Thanks for the help!

    http://wordpress.org/extend/plugins/types/

  2. klongdesigns
    Member
    Posted 2 years ago #

    Looking at this, you are setting the variable in the line before, even though there may be nothing in it...Is use this to check if a field it set:

    if ($prop_city_info != '') {

    Since I set it in the line above, the isset will always return true. However, using the != '' checks if it has anything in it, which a custom field will have if there is anything there.

  3. Eric_ebdesigns
    Member
    Posted 2 years ago #

    I'm having some issues trying to accomplish the same thing.

    Here is the relevant code:

    $advertisement1 = types_render_field("top-first-ad-space", array("raw"=>"true"));
    if ($advertisement1 != ''){/*do nothing*/}
    	else{unset($advertisement1);}
    
    if (isset($advertisement1)){$advertisement1  = str_replace( '<br />', '', str_replace( '<br>', '', str_replace( '<br/>', '', $advertisement1 ) ) );}
    
    <?php if (isset($advertisement1)){}else{echo 'class=\"no-ads\"';} ?>

    An explanation of the code:
    1) Calling the meta information
    2) Testing if the meta data has a value, if it doesnt then unset the variable
    3) if the varialbe is set, strip the variable of line breaks (i'm calling a field which outputs raw html, and wordpress likes to inject s in the meta field if there is a hard break entered in the GUI
    4) The last code is inline in a div. If the variable is set, do nothing. otherwise, add the class 'no-add's

    If anyone could offer any help (or would know how to clean up my code), I would appreciate it. I've only been coding php for two months.

  4. iamSeanGrant
    Member
    Posted 2 years ago #

    Ahhh! Yes!
    This worked.
    if ($prop_city_info != '')

    Duh, I guess.

    Thanks klongdesigns!

    @ Eric_ebdesigns
    I'm not sure how well isset works here, so I'd suggest changing your if statement for striping of line breaks from isset to:
    if ($prop_city_info != '')
    unless you need isset for some reason I don't know about. :)

    Thanks to all!

  5. AmirHelzer
    Member
    Plugin Author

    Posted 2 years ago #

    Great. Let's mark it as 'resolved'.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic