Tell us what you are trying to do?
I am using a shortcode snippet that was provided to add comma separators in numbers. I am currently using this on several number fields in one custom post type (CPT1) and it works fine. However, when I use the same shortcode on number fields on a different custom post type (CPT2), I receive a Fatal PHP error when viewing on the front end.
This is the code snippet that I use. It is placed in the Toolset, Settings, Custom Code tab.
<?php
// Put the code of your snippet below this comment.
/**
* Adds comma to number with no decimal places.
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Custom shortcode to display formatted number
function wp_format_number( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'number' => '',
),
$atts
);
return number_format($atts['number'],0,'.',',');
}
add_shortcode( 'number_0', 'wp_format_number' );
This is how it is used on a number custom field that works on CPT1.
Asking Price: $[number_0 number='[types field='asking-price' format='FIELD_VALUE'][/types]']
Here is how it is used on a number custom field that throws the PHP error on CPT2.
Cash Required: $[number_0 number='[types field='asking-price' format='FIELD_VALUE'][/types]']
Here is the first lines of the Fatal PHP error.
Fatal error: Uncaught Error: number_format(): Argument #1 ($num) must be of type float, string given
in /www/323lifeworks_849/public/wp-content/toolset-customizations/comma_decimal_number_format.php on line 21
There are 16 items listed in the Call stack: below this error.
The use as shown on CPT2 above is entered on a field on line 20 of a Content Template.
Is there any documentation that you are following?
This is the support ticket used as a guide.
https://toolset.com/forums/topic/comma-decimal-format-for-numbers-us-format/
Is there a similar example that we can see?
What is the link to your site?
This is a post that uses the Content Template for CPT1 above that does NOT result in the Fatal PHP error.
hidden link
This is a post that uses the Content Template for CPT2 above that does result in the Fatal PHP error.
hidden link
What would cause this, and how would I resolve it?