Table of Contents:
I was working on a project where I needed to store a number in custom field. But on the front end, I wanted the number to have comma separators to look like a normal number. E.g 125,000.
In this post, we will quickly look at how to convert a raw number stored in the WordPress database to a number with comma separators in Bricks Builder.
Use The Bricks Builder Echo Functionality To Return A Formatted Number
The fastest way to format a number is to use the number_format function inside Bricks Builder. This function will add commas and decimals to a value.
In my use case, I was using whole numbers like 500,000.
To format the number, you can use the text block element or any other element that can access dynamic data. This bit a code below will format the number by adding a comma separator.
${echo:number_format({your_custom_field})}
Steps To Format A Number With Comma Separator In Bricks Builder
Step 1: Create a custom Field to store your data.
Step 2: In Bricks Builder, add a text element and delete the dummy text.
Step 3: Select the dynamic data lightning bolt icon
Step 4: Pick the custom field that has your data in number format {your_custom_field}
Step 5: wrap the custom field with and place your custom field inside the parenthesis
It should look like this:
${echo:number_format({your_custom_field})}
FAQs
Can I format numbers with decimals using this method?
Yes! The number_format
function can also handle decimals. By default, it formats numbers without decimals, but you can specify the number of decimal places you want. For example, {echo:number_format({your_custom_field}, 2)}
would format a number with two decimal places.
Is it possible to format numbers with other separators, like spaces or dots?
From my experience, yes. . The number_format
function allows you to specify the decimal and thousands separators. For example, {echo:number_format({your_custom_field}, 0, '.', ' ')}
would format a number using spaces as thousands separators.
What if my custom field contains a value that is not a number?
If the custom field value is not numeric, number_format
will return 0. Ensure your custom field only contains numerical values to avoid unexpected results!
Conclusion
Formatting numbers with comma separators in Bricks Builder enhances readability and professionalism in presenting data. By leveraging the PHP number_format
function within Bricks Builder’s dynamic content system, you can easily display numbers in a more user-friendly format. This technique is particularly useful for websites that deal with financial data, statistics, or any context where large numbers are common. With the steps outlined above, you can improve the clarity and aesthetic of your numerical data on your WordPress site.