Forums

[resolved] How to check for a custom field? (5 posts)

  1. brian914
    Member
    Posted 1 year ago #

    I am new to wordpress, so I have the following question, regarding if this is the right way to do this. It seems to work in my context, but I am curious, if there are better ways to do this?

    I am using wordpress as a cms and have a section of portfolio pages. Some of them have urls to projects and some don't have that. So I have the following to check if that custom field is there:

    if ( get_post_meta($post->ID, 'project_url', true) != "" ) {
    	// Add link here
    };

    Is this how I would do this?

    Thanks a lot for any advice on this!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  3. brian914
    Member
    Posted 1 year ago #

    Thank you!

    I guess this would be more correct?

    if ( get_post_meta($post->ID, 'project_url', true) != false ) { ....

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Yes. Or just if ( get_post_meta($post->ID, 'project_url', true) ) {... should work.

  5. brian914
    Member
    Posted 1 year ago #

    haha, yeah, that seems cleaner for sure!

    Thank you very much!!!

Topic Closed

This topic has been closed to new replies.

About this Topic