• Resolved double_dd

    (@double_dd)


    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.
    • This topic was modified 7 years, 7 months ago by double_dd.
Viewing 6 replies - 1 through 6 (of 6 total)
  • 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

    (@double_dd)

    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.

    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

    (@double_dd)

    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!

    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

    Thread Starter double_dd

    (@double_dd)

    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.