在我的網站上,我打算顯示網站上的帖子和評論總數,以及從我的網站購買的總數。我寫的代碼如下:
//copy to functions.php
// Total Comment
function site_total_comment_count() {
$num_comm = get_comment_count();
$num_comm = $num_comm['total_comments'];
echo $num_comm ;}
add_shortcode('total_comment_count', 'site_total_comment_count');
// Total Completed Orders
function total_completed_Orders() {
$query = new WC_Order_Query( array(
'limit' => 99999,
'status' => array( 'completed' ),
'return' => 'ids',
) );
$orders = $query->get_orders();
return count( $orders ); }
// Copy to the desired page
<h2> All Orders:
<?php echo total_completed_Orders(); ?>
</h2>
<h2> All Comments:
<?php echo site_total_comment_count(); ?>
</h2>
<h2> All Posts:
<?php
echo $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
?>
</h2>
這些代碼單獨運行良好,但是當我將所有三個代碼都放在目標頁面上時,統計資料顯示錯誤。
你能給我寫一段代碼來顯示我網站上這三項的正確統計資訊嗎?
uj5u.com熱心網友回復:
您可以創建自定義簡碼。
在你的 functions.php 檔案中試試這個:
add_shortcode('custom_shortcode', 'kb_get_details');
function kb_get_details()
{
//For total post.
global $wpdb;
$total_post = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
//For total comments.
$num_comm = get_comment_count();
$total_cmt = $num_comm['total_comments'];
//For total orders.
$query = new WC_Order_Query(array(
'limit' => 99999,
'status' => array('completed'),
'return' => 'ids',
));
$orders = $query->get_orders();
$total_orders = count($orders);
?>
<h2>All Posts : <?php esc_html_e($total_post); ?></h2>
<h2>All Comments: <?php esc_html_e($total_cmt); ?></h2>
<h2>All Orders : <?php esc_html_e($total_orders); ?></h2>
<?php
}
之后,這個短代碼可以直接從后端添加到你的目標頁面。您還可以通過使用將其添加到任何自定義模板do_shortcode('[custom_shortcode]');
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533652.html
上一篇:如何在WooCommerce中僅為非變體產品顯示自己的資料
下一篇:如何加載兩個QML合并兩個專案
