WordPress.org

Ready to get started?Download WordPress

Forums

Using Multiple Custom Fields in IF Statements (2 posts)

  1. sarwic
    Member
    Posted 9 months ago #

    I was wondering if anyone knew a clean way to use multiple custom fields in an IF statement.

    At the moment I have it spaced out, so each custom field "SponsorHeading#" has it's own if/else statement:

    if(get_post_meta($post_id, 'SponsorHeading1', true)) {
           echo '<h2>Summit Sponsors </h2>';
           			}
           			else {
    	       			echo '';
           			}
           			if(get_post_meta($post_id, 'SponsorHeading2', true)) {
           			echo '<h2>Summit Sponsors </h2>';
           			}
           			else {
    	       			echo '';
           			} 
    
    and so on for 3 more custom fields. I'd like to have something cleaner like:
    
    if(get_post_meta($post_id, 'SponsorHeading2', true)) || if(get_post_meta($post_id, 'SponsorHeading2', true)) || if(get_post_meta($post_id, 'SponsorHeading2', true)) {
          echo '<h2>Summit Sponsors </h2>';
    }
         else {
                echo '';
    }

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    or something along those lines to clean it up but nothing I've tried has worked.

    Any suggestions?

  2. sarwic
    Member
    Posted 9 months ago #

    if(get_post_meta($post_id, 'SponsorHeading1', true)) {
           echo '<h2>Summit Sponsors </h2>';
           			}
           			else {
    	       			echo '';
           			}
           			if(get_post_meta($post_id, 'SponsorHeading2', true)) {
           			echo '<h2>Summit Sponsors </h2>';
           			}
           			else {
    	       			echo '';
           			}

    and so on for 3 more custom fields. I'd like to have something cleaner like:

    if(get_post_meta($post_id, 'SponsorHeading2', true)) || if(get_post_meta($post_id, 'SponsorHeading2', true)) || if(get_post_meta($post_id, 'SponsorHeading2', true)) {
          echo '<h2>Summit Sponsors </h2>';
    }
         else {
                echo '';
    }

Reply

You must log in to post.

About this Topic