Home › Forums › Front-end Issues › Displaying Fields – HTML in functions.php
Hi,
I have 6 custom fields: link1, price1, link2, price2, link3, price3
I need to check if data exists in each set and then display them as one (using a shortcode). I’m currently putting the code into the functions.php file but I’m a bit lost if I’m honest.
This is what I have so far using only 2 custom fields:
function displayprice() {
$go_price = get_field( "price1" );
$go_link = get_field( "link1" );
if( $price1 & $link1 ) {
echo '<a href='.$link1;'>'.$price1;'</a>';
}
}
add_shortcode('showprices', 'displayprice');
I believe I’m getting mixed up with the html and the retrieval of the variables within it.
Any help appreciated 🙂
Shortcodes are meant to return data instead of echo it out. Additionally, it looks like the concatenation is malformed after $link1
. Any time you’re combining variables with strings you need to use period .
to connect them and only a semicolon at the very end. The variables are also not right – you use ‘price1’ in for the field parameter but $go_price
is the variable. The anchor needs double quotes to surround the attribute URL. And the conditional needs 2 ampersands.
All in all it should look something like this:
function displayprice() {
$return = '';
$go_price = get_field( 'price1' );
$go_link = get_field( 'link1' );
if( $go_price && $go_link ) {
$return = '<a href="' . esc_url( $go_link ) . '">' . $go_price . '</a>';
}
return $return;
}
add_shortcode( 'showprices', 'displayprice' );
That works perfectly – thank you.
One more thing if possible, how would I combine multiple variables so it can run through price and link 1, 2 and 3 like below:
If there is data in price1/link1 = show
If there is data in price2/link2 = show
If there is data in price3/link3 = show
else
return ‘Not Available’
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' );
Unfortunately my variables are labelled textually otherwise this would work.
So Go_Price/Go_Link, BK_Price/BK_Link, ML_Price/ML_link
I assume this would change the solution completely?
It would, just use the same method as the first with 4 conditionals
$price1 = get_field( 'foo' );
$link1 = get_field( 'bar' );
if( $price1 && $link1 ) {
$return .= '<a href="' . esc_url( $link1 ) . '">' . $price1 . '</a>';
}
If none of the conditionals run, $return
will be empty and you can check against that to return you default string.
This is what I have so far:
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' );
The issue I have is that if the first conditional is true, it doesn’t return any other. I need all 3 to return if data exists in each.
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.