$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Insert Commas into ACF number field|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Insert Commas into ACF number field

matteradmin8PV0评论

I am displaying numbers (non currency) via an advance custom field of paid_attendance - however by default number fields in ACF do not include commas or decimals etc.

I would like for commas to be included when the numbers from paid_attendance are output to the user on the front end. I am aware this can be done through the template file but I need to make it global via functions.php. I am probably way off, but below is the code I put together. Any help would be greatly appreciated.

// Return number with commas
function fix_number( $wpdb ) {
$number = get_field('paid_attendance');
number_format($number)."<br>";
}
add_action('fix_number');

EDIT Below is the fix and I have now got it working. Clearly and as Rick mentioned I was not using hooks properly. After reading deeper into the ACF docs and consulting other users on their forum, I was able to do what I need.

add_filter('acf/format_value/name=paid_attendance', 'fix_number', 20, 3);
function fix_number($value, $post_id, $field) {
  $value = number_format($value);
  return $value;
}

I am displaying numbers (non currency) via an advance custom field of paid_attendance - however by default number fields in ACF do not include commas or decimals etc.

I would like for commas to be included when the numbers from paid_attendance are output to the user on the front end. I am aware this can be done through the template file but I need to make it global via functions.php. I am probably way off, but below is the code I put together. Any help would be greatly appreciated.

// Return number with commas
function fix_number( $wpdb ) {
$number = get_field('paid_attendance');
number_format($number)."<br>";
}
add_action('fix_number');

EDIT Below is the fix and I have now got it working. Clearly and as Rick mentioned I was not using hooks properly. After reading deeper into the ACF docs and consulting other users on their forum, I was able to do what I need.

add_filter('acf/format_value/name=paid_attendance', 'fix_number', 20, 3);
function fix_number($value, $post_id, $field) {
  $value = number_format($value);
  return $value;
}
Share Improve this question edited Feb 1, 2019 at 4:54 James asked Jan 31, 2019 at 19:01 JamesJames 211 silver badge9 bronze badges 1
  • I would try ACF's support channels as they can tell you how best to format globally. I believe you can add a filter to get_field so if the field name is paid_attendance you can take the value and format as desired, which will then apply everywhere. – WebElaine Commented Jan 31, 2019 at 19:36
Add a comment  | 

1 Answer 1

Reset to default 2

You need to 'hook' your function to a valid hook. Your add_action call is not doing anything, because it is not a proper call to a hook.. See the docs https://developer.wordpress/reference/functions/add_action/ for add_action.

Your function is the second parameter of the add_action hook. The first parameter is 'where' to hook into WordPress. If you want to modify the post content, then hook into the_content (see https://developer.wordpress/reference/hooks/the_content/ ).

But, then you need to parse the_content to find a number to format. The the_content hook works on the entire post (or page) content, so your function won't work.

So, not sure that you can do what you want. But if you are going to use hooks, then you need to learn how they work. The above links are a starting point.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far