PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 2, 2022

[FIXED] Displays the total number of products in Wordpress

 March 02, 2022     display, php, product, woocommerce, wordpress     No comments   

Issue

How do I get Wordpress to display the result (number of all products in the shop) exactly where I want it to be. So far I have inserted the code (see below) into functions.php and the result is also displayed, but top left on all pages... I only need to display this in a special page. something like that: today we have a total of XXX products in our database.

Thanks for your support!!

My question refers to this post and i was prompted to ask a new question. Displays the total number of products

This is the code:

'posts_per_page' => -1 );
$products = new WP_Query( $args );
echo $products->found_posts;

Solution

This depends as how your theme is coded but one thing you could do is to the same thing but in a Shortcode, if you add something like this in your 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' );

Then in the content of the page you want to show the message just add

[total_products]

This should display the text with the total count of products



Answered By - Pol
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing