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' );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.