Hello!
I need help with a text input field, because I need to add $ if is a number and dots if is thousand ($123.456)
Here is my code
<?php if ( have_rows( 'valores' ) ) : ?>
<?php while ( have_rows( 'valores' ) ) : the_row(); ?>
<?php the_sub_field( 'valor1' ); ?>
<?php the_sub_field( 'valor2' ); ?>
<?php endwhile; ?>
<?php endif; ?>
I’ll appreciate any help
Thank you very much!
$valor1 = get_sub_field('valor1');
if (intval($valor1) == $valor1) {
$valor1 = '$'.number_format(intval($valor1, ',', '.'));
}
echo $valor1;
What is the value that is in the field that you’re trying to convert?
valor1= 260000 –> $260.000
valor2= example –> example
valor1 and valor2 can be number or text.
<?php if ( have_rows( 'valores' ) ) : ?>
<?php while ( have_rows( 'valores' ) ) : the_row(); ?>
<?php the_sub_field( 'valor1' ); ?>
<?php the_sub_field( 'valor2' ); ?>
<?php endwhile; ?>
<?php endif; ?>
Thx
I want to know what you changed your code to after my first post, you re-posted the original code.
With the next code, I have $0
<?php $valor1 = get_sub_field('valor1');
if (intval($valor1) == $valor1) {
$valor1 = '$'.number_format(intval($valor1, ',', '.'));
}
echo $valor1;
?>
With the next code, I have: 260000$0
<?php if ( have_rows( 'valores' ) ) : ?>
<?php while ( have_rows( 'valores' ) ) : the_row(); ?>
<?php
$valor1 = the_sub_field('valor1');
if (intval($valor1) == $valor1) {
$valor1 = '$'.number_format(intval($valor1, ',', '.'));
}
echo $valor1;
?>
<?php endwhile; ?>
<?php endif; ?>
Thx
if ( have_rows( 'valores' ) ) :
while ( have_rows( 'valores' ) ) :
the_row();
$value = get_field('valor1');
if ($value == intval($value)) {
$value = '$'.number_format(intval($value, ',', '.'));
}
echo $value;
$value = get_field('valor2');
if ($value == intval($value)) {
$value = '$'.number_format(intval($value, ',', '.'));
}
echo $value;
endwhile;
endif;
Hello John,
It still doesn’t work =(
with this: $value = get_field(‘valor1’);
I have: $0
With this: $value = the_sub_field(‘valor1’);
I have: 260000$0
Any idea?
By the way, thank you very much for your time
if ( have_rows( 'valores' ) ) :
while ( have_rows( 'valores' ) ) :
the_row();
$value = get_sub_field('valor1');
if ($value == intval($value)) {
$value = '$'.number_format(intval($value, ',', '.'));
}
echo $value;
$value = get_sub_field('valor2');
if ($value == intval($value)) {
$value = '$'.number_format(intval($value, ',', '.'));
}
echo $value;
endwhile;
endif;
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’re working hard on Advanced Custom Fields PRO 6.0, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) August 12, 2022
Featuring improved performance for Repeater fields with large datasets, and a new generation of ACF Blocks.
Let’s take a look 🧵https://t.co/Befre3kFAo
© 2022 Advanced Custom Fields.
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.