Displaying Category Name and Post Count on Category Archive Page

63

July 31, 2024

August 27, 2024

00:00:00

Today:Saturday

2024/06/05

  • home
  • Wordpress
  • Displaying Category Name and Post Count on Category Archive Page

Estimated reading time: 6 minutes


Font Size

Excerpt from this article:

This article delves into a code snippet designed to calculate and display the post count for each category within a WordPress website. The code incorporates three primary functions: get_category_by_name for retrieving category information based on its name, get_category_post_count for computing the post count within a category, and a while loop to iterate through posts and display the post count for each category. Additionally, the article provides explanations for the functions and the underlying logic of the code.

  • Supporting us, a step towards progress!

    • With your help, we can continue our journey of providing high-quality and valuable content. Your support, no matter how small, gives us the motivation and power to take firmer steps on this challenging path.


      Two simple ways to support us:
      1. Contribute to cryptocurrency mining: Share some of your system's processing power to help us mine cryptocurrencies.
      2.  Send cryptocurrency: You can directly send cryptocurrency to our wallet.


      With your support, a world of useful information will be available to everyone!

To get to know us better, check out our About Us page.

We dedicate countless hours and energy to creating high-quality, informative content for you. Your support is what motivates us to continue this journey.

We sincerely appreciate your support.

1,000,000 Hashes (Total) = 0.000,000,03 BTC

1 BTC BitTorrentBTTC

2 BTC BEP-20PEPE

3 BTC ShibaSHIB

4 BTC WINkLinkWIN

5 BTC RadiocacaRACA

6 BTC FlokiFLOKI

7 BTC EverdomeDOME

8 BTC DentDENT

9 BTC ReefREEF

10 BTC HoloHOT

11 BTC LeverFiLEVER

12 BTC AmpAMP

13 BTC Reserve rightsRSR

14 BTC AnkrANKR

15 BTC Beta FinanceBETA

16 BTC TRONTRX

17 BTC DogecoinDOGE

18 BTC TetherUSDT

19 BTC Chorono.techTIME

20 BTC CompoundCOMP

21 BTC BitcoinCashBCH

22 BTC BinanceCoinBNB

23 BTC EthereumETH

24 BTC BitcoinBTC

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the TRON network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the DOGECOIN network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOINCASH network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the ETHEREUM network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOIN network.

Please deposit BTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTC only to this address.

Full content of the article:

By default, WordPress offers a shortcode to display the category name on category.php or archive.php pages:

<?php single_cat_title( 'Category: ', true ); ?>

 

Alternatively, this shortcode can be customized as follows:

<?php
$category = get_queried_object();
echo $category->name;
?>

 

In both scenarios, the category name is retrieved on category.php or archive.php pages.

**

To enhance the professionalism of your WordPress theme, consider displaying the number of posts associated with each category alongside the category name.

**

To achieve this, we’ll need to extend WordPress’s default shortcodes.**

Below is the complete code for displaying the category name and its corresponding post count, along with a concise explanation.

**

Note: The provided code should be placed within category.php or archive.php to avoid errors.

**

Code Explanation:

The presented code is responsible for calculating and displaying the post count for each category on a WordPress category archive page. It leverages three core functions:**

get_category_by_name($category_name):
This function retrieves a category based on its name. The $category_name parameter accepts the desired category name. The function initially uses the get_categories function to obtain a list of categories based on their names, excluding hidden categories. It then returns the first category from the list or returns null if the category is not found.

**

**● get_category_post_count($category_name):
This function calculates the post count within a category. The $category_name parameter accepts the desired category name. It first utilizes the get_category_by_name function to retrieve category information. Subsequently, it extracts the category’s post count using the count property of the category object and returns it. If the category is not found, it returns zero.

**

**● while loop:
This loop is employed to iterate through posts on the category archive page. In each iteration, the current category’s name and post count are computed. If the current category differs from the previous category, the previous category’s post count is displayed, and the current category’s information is stored as the previous category. At the end of the loop, the post count for the last category is also displayed.

 

Additional Explanations:

● The single_cat_title() function is used to retrieve the current category’s name.

**● The variables $previous_category_name and $previous_post_count are utilized to store the previous category’s information.

**● The final output is displayed within an HTML element with the class SC_cat_name_post_count.

**

This code is valuable for displaying the post count for each category on a WordPress category archive page, thereby enhancing the user experience.

 

<!-- This code should be placed in category.php and archive.php files. -->
<h2 id="SC_cat_name_count">
<?php
// Function to retrieve a category by its name
function get_category_by_name($category_name) {
  $categories = get_categories(array('name' => $category_name, 'hide_empty' => false));
  if ($categories) {
    return $categories[0];
  } else {
    return null;
  }
}
// Assuming you have the get_category_post_count() function defined elsewhere (e.g., functions.php)
function get_category_post_count($category_name) {
  $category = get_category_by_name($category_name);
  if ($category) {
    return $category->count;
  } else {
    return 0;
  }
}
if ( have_posts() && is_category() ) {
  $previous_category_name = ''; // Initialize the previous category name
  while ( have_posts() ) : the_post();
    $category_name = single_cat_title( '', false ); // Get category name
    $post_count = get_category_post_count($category_name);

    if ($category_name != $previous_category_name) { // Check if it's a new category
      if ($previous_category_name) { // Print previous category if not empty
        echo '<span class="SC_cat-name_post-count">' . $previous_category_name . ': ' . '<em>' . $previous_post_count . '</em>' . ' posts</span>';
      }
      $previous_category_name = $category_name; // Set the current category as previous
      $previous_post_count = $post_count; // Set the current post count as previous
    } else {
      $previous_post_count = $post_count;
    }
  endwhile; // End the loop
  // Print the last category if not printed already
  if ($previous_category_name) {
    echo '<span class="SC_cat-name_post-count">' . $previous_category_name . ': ' . '<em>' . $previous_post_count . '</em>' . ' posts</span>';
  }
}
?>
</h2>

You can download the complete and tested code from the following link:

Download

Archive ,  Articles ,  Category ,  PHP ,  Wordpress .

 ,   ,   ,   ,   ,   ,   .

Comment

[php] Your code [/php]

Leave a Reply

Your email address will not be published.Required fields are marked *

  • All Our Articles

    • To provide you with an overview of all our published articles, we have created this section. In our articles, we will focus on the practical application of the material, rather than just filling the site with content. Therefore, our articles will be few but very practical. Our articles will cover a wide range of technology and programming topics.

UTC Time Zone
Read More
Fetching database visit statistics using PHP without plugins
Read More
Examination of title code within WordPress templates
Read More
Adding a Period to Categories in WordPress: A Simple and Effective Solution
Read More
Retrieving and Displaying a Complete List of WordPress Site Categories
Read More
Displaying Category Name and Post Count on Category Archive Page
Read More
Showing accurate post view counts on archive pages
Read More

Fonts made from Web Fonts is licensed by CC BY 4.0

13,092