Home > OS >  How to change format number in PHP
How to change format number in PHP

Time:10-25

I have the following views count function for wordpress post. However, the number that comes up is for example 2000, while I'm trying to get 2.000.

Located in Functions.php

add_action ('elementor/query/popular_post', function ($query) {
    $query->set('orderby', 'meta_value_num');
    $query->set('meta_key', 'views');
});

Located in Header.php

<?php
if (is_singular('post')) {
    $count = get_field('views');
    $count   ;
    update_field('views', $count);
}
?>

I tried googling and changed the code several times but couldn't change the format of the numbers. Would anyone be kind enough to give a Tips?

I'm sorry for not being able to replicate the function in JsFiddle or similar, but I believe this is not possible.

I don't know php, I'm a fan.

CodePudding user response:

You can use number_format() function here: www.w3schools.com/php/func_string_number_format.asp

See last example here where they set for as thousands separator: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_number_format

Your code updated as below I believe:

update_field('views', number_format($count,0,",","."));

CodePudding user response:

See the reference on PHP: number_format

  • Related