Support

Account

Home Forums Front-end Issues Displaying Fields – HTML in functions.php Reply To: Displaying Fields – HTML in functions.php

  • If you’re variables are named like above you could loop through them with a for() loop and take advantage of the $i incrementor:

    function displayprice() {
    
    	$return		= '';
    	
    	for( $i = 1; $i <= 3; $i++ ) {
    		
    		$price = get_field( "price{$i}" );	// price1, price2, price3
    		$link  = get_field( "link{$i}" );	// link1, link2, link3
    		$return .= sprintf(
    			'<a href="%1$s" class="%2$s">%3$s</a>',
    			esc_url( $link ),
    			"price{$i}",
    			$price
    		);
    		
    	}
    	
    	if( empty( $return ) ) {
    		$return = __( 'Not Available' );
    	}
    
    	return $return;
    	
    }
    add_shortcode( 'showprices', 'displayprice' );