显示 Wordpress 中的产品总数
Displays the total number of products in Wordpress
如何让 Wordpress 将结果(商店中所有产品的数量)准确显示在我想要的位置。到目前为止,我已经将代码(见下文)插入 functions.php 并且结果也显示出来,但是在所有页面的左上角......我只需要在一个特殊的页面中显示它。类似这样的事情:今天我们的数据库中共有 XXX 产品。
感谢您的支持!!
我的问题是指这个 post,系统提示我问一个新问题。
这是代码:
'posts_per_page' => -1 );
$products = new WP_Query( $args );
echo $products->found_posts;
这取决于您的主题是如何编码的,但是您可以做的一件事是在简码中做同样的事情,如果您在 functions.php
中添加类似的内容
function total_products_func(){
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1
);
$products = new WP_Query( $args );
return "Today we have a total of " . $products->found_posts . "products in our database";
}
add_shortcode( 'total_products', 'total_products_func' );
然后在要显示消息的页面内容中添加
[total_products]
这应该显示包含产品总数的文本
您实际上可以使用 wp_count_posts() 来简化它。
https://developer.wordpress.org/reference/functions/wp_count_posts/
function total_products_func() {
$count = wp_count_posts('product');
return "Total products on hand: " .$count->publish;
}
add_shortcode( 'total_products', 'total_products_func' );
但是,正如 Pol 指出的那样,简短的代码是最简单的方法并且可以更新以允许您即时自定义消息。同样,您甚至可以使用它轻松提取您拥有的任何 post 类型的总数。
如何让 Wordpress 将结果(商店中所有产品的数量)准确显示在我想要的位置。到目前为止,我已经将代码(见下文)插入 functions.php 并且结果也显示出来,但是在所有页面的左上角......我只需要在一个特殊的页面中显示它。类似这样的事情:今天我们的数据库中共有 XXX 产品。
感谢您的支持!!
我的问题是指这个 post,系统提示我问一个新问题。
这是代码:
'posts_per_page' => -1 );
$products = new WP_Query( $args );
echo $products->found_posts;
这取决于您的主题是如何编码的,但是您可以做的一件事是在简码中做同样的事情,如果您在 functions.php
中添加类似的内容function total_products_func(){
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1
);
$products = new WP_Query( $args );
return "Today we have a total of " . $products->found_posts . "products in our database";
}
add_shortcode( 'total_products', 'total_products_func' );
然后在要显示消息的页面内容中添加
[total_products]
这应该显示包含产品总数的文本
您实际上可以使用 wp_count_posts() 来简化它。
https://developer.wordpress.org/reference/functions/wp_count_posts/
function total_products_func() {
$count = wp_count_posts('product');
return "Total products on hand: " .$count->publish;
}
add_shortcode( 'total_products', 'total_products_func' );
但是,正如 Pol 指出的那样,简短的代码是最简单的方法并且可以更新以允许您即时自定义消息。同样,您甚至可以使用它轻松提取您拥有的任何 post 类型的总数。