Title: Create Function &#8211; Multiple IFs return custom fields
Last modified: September 18, 2018

---

# Create Function – Multiple IFs return custom fields

 *  Resolved [double_dd](https://wordpress.org/support/users/double_dd/)
 * (@double_dd)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/)
 * Hi,
 * I’m trying to create a function to query 6 custom fields (3 pairs) I have to 
   determine if they have data. If they do, then I’d like to return that data (price
   and link) and output it onto the page. The final bit would be putting this function
   into a shortcode I can then use across the theme.
 * The customs fields are: go_price, go_link, bk_price, bk_link, ml_price, ml_link
 * My current function is:
 *     ```
       function displayprice() {
   
       	$return		= '';
       	$go_price 	= get_field( 'go_price' );
       	$go_link 	= get_field( 'go_link' );
               $bk_price 	= get_field( 'bk_price' );
       	$bk_link 	= get_field( 'bk_link' );
               $ml_price 	= get_field( 'ml_price' );
       	$ml_link 	= get_field( 'ml_link' );
   
       	if( $go_price && $go_link ) {
       		$return = '<a href="' . esc_url( $go_link ) . '">' . $go_price . '</a>';
       	}
   
               if( $bk_price && $bk_link ) {
       		$return = '<a href="' . esc_url( $bk_link ) . '">' . $bk_price . '</a>';
       	}
   
               if( $ml_price && $ml_link ) {
       		$return = '<a href="' . esc_url( $ml_link ) . '">' . $ml_price . '</a>';
       	}
   
       	return $return;
   
       }
       add_shortcode( 'showprices', 'displayprice' );
       ```
   
 * I’ve obviously got something wrong somewhere. It’s returning the first value 
   but doesn’t return the rest. Could someone point me in the right direction?
 * Many thanks
    -  This topic was modified 7 years, 7 months ago by [double_dd](https://wordpress.org/support/users/double_dd/).
    -  This topic was modified 7 years, 7 months ago by [double_dd](https://wordpress.org/support/users/double_dd/).

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Safeer](https://wordpress.org/support/users/safeerz/)
 * (@safeerz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10698718)
 * You most likely want to do else if for the 2nd ifs or have a way to know if none
   of the are true, do the else statement
 *  Thread Starter [double_dd](https://wordpress.org/support/users/double_dd/)
 * (@double_dd)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699015)
 * Else ifs don’t enable me to show all the if statements that are true though? 
   I shouldn’t have to run through every combination of the 3 pairs having data 
   or not to generate the function surely.
 *  [Safeer](https://wordpress.org/support/users/safeerz/)
 * (@safeerz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699115)
 * So if all the conditions are true, you want to output 3 links?
 * Current code supposed to output link for the last true statement. no idea why
   it is returning only first. To output all links, you need to concatenate $return
   for last two statement like
 * `$return .= '<a href="' . esc_url( $bk_link ) . '">' . $bk_price . '</a>';`
 *  Thread Starter [double_dd](https://wordpress.org/support/users/double_dd/)
 * (@double_dd)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699300)
 * > So if all the conditions are true, you want to output 3 links?
 * Yep this is exactly what I want.
 * I’m a little confused what do you mean I need to concatenate for the last statement?
 * Could you copy my original code and edit to show what you mean?
 * Thanks!
 *  [Safeer](https://wordpress.org/support/users/safeerz/)
 * (@safeerz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699340)
 * Hello,
 * I have already edited the line above, if you look close, there is `.` before 
   =
    This will add new links to the existing codes. refer to php doc for details
   [http://php.net/manual/en/language.operators.string.php](http://php.net/manual/en/language.operators.string.php)
 *  Thread Starter [double_dd](https://wordpress.org/support/users/double_dd/)
 * (@double_dd)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699412)
 * Thanks Safeer that works perfectly.
 * Who knew it would be something so small!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Create Function – Multiple IFs return custom fields’ is closed to new
replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 2 participants
 * Last reply from: [double_dd](https://wordpress.org/support/users/double_dd/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/create-function-multiple-ifs-return-custom-fields/#post-10699412)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
