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>
Comment
[php] Your code [/php]