2013-04-09 27 views
0

這是我面臨的問題。我正在使用Wordpress,並使用兩個PHP在我的網站上向客戶顯示統計信息,例如網站上的# total of posts已創建和# of total comments。 PHP的工作效果很好,並且顯示了當前實時#,但是它們沒有渲染逗號,例如2,700,其中顯示爲2700並且對於另一示例100,800來說,其顯示爲100800

誰能告訴我如何修改這2個PHP的實現?

下面是帖子的狀態PHP代碼:

<?php 
$postcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); 
echo $postcount; 
?> 

Here is the comments status php: 

<?php 
$comments_count = wp_count_comments(); 
echo "" . $comments_count->total_comments . ""; 
?> 

在此先感謝。

-Jay

回答

1
<?php 
    $postcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); 
    echo number_format($postcount, 0); 
?> 

在評論數:

<?php 
    $comments_count = wp_count_comments(); 
    echo "" . number_format($comments_count->total_comments, 0) . ""; 
?> 
+0

「,0」太多(=>不必要);它已經是默認參數。 – bwoebi 2013-04-09 20:37:44

+0

正確 - 這不是必需的,只是習慣的強制。 – nickhar 2013-04-09 20:39:53

+0

那個很棒。我如何在第二個PHP中獲得此評論#? – Jay 2013-04-09 20:40:58

1

您正在尋找:http://php.net/manual/en/function.number-format.php

number_format($postcount); 

應該足以顯示千位分隔符。

對於第二個用途:

echo "" . number_format($comments_count->total_comments) . ""; 
+0

不錯,我讓他們現在都工作,並瞭解更好的分配如何實現這一目標下一次。非常感謝大家 – Jay 2013-04-09 20:45:10