Adding a Period to Categories in WordPress: A Simple and Effective Solution

79

July 30, 2024

August 27, 2024

00:00:00

Today:Saturday

2024/06/05

  • home
  • Wordpress
  • Adding a Period to Categories in WordPress: A Simple and Effective Solution

Estimated reading time: 4 minutes


Font Size

Excerpt from this article:

This article delves into a concise and practical piece of WordPress code that automatically appends a period to the end of category names on single post pages. This subtle enhancement can improve the visual appeal and readability of category lists. We will also examine the code’s functionality and its core functions to gain a deeper understanding of its workings.

  • 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:

WordPress, as a popular content management system, provides extensive customization options for users. One of these features allows for tailoring the appearance and content of different website pages. In this article, we will explore a simple trick to enhance the visual appeal of category lists on single post pages.

 

Code Analysis:

The provided code defines a custom function named add_dot_to_categories. This function serves as a filter for WordPress’s the_category function. The the_category function, by default, displays the list of categories associated with a post. Our filter intercepts this list and modifies it by appending a period to the end of each category.

 

How the Code Works:

Function Definition: The add_dot_to_categories function accepts a parameter named $categories that contains the list of post categories.

**

Data Type Check: It initially checks if $categories is a string or an array. If it’s a string, it’s converted into an array using the explode function.

**

Output Creation: A variable named $output is declared to store the final output.

**

foreach Loop: A foreach loop iterates through each element (category name) within the $categories array.

**

Adding a Period: In each iteration, a period is added to the end of the category name and appended to the $output variable.

**

Removing Extra Separator: Finally, any extra separator at the end of the $output string is removed.

**

Returning Output: The final $output value, containing the category list with periods, is returned.

**

Applying the Filter: Using the add_filter function, our filter is attached to the the_category function. Consequently, whenever the the_category function is invoked, its output is modified by our filter.

 

Key Functions Used in the Code:

is_string(): This function is used to check the data type and determines if a variable is a string.

explode(): This function converts a string into an array based on a specified delimiter.

foreach: This control structure is used to iterate over the elements of an array.

rtrim(): This function removes specified characters from the end of a string.

add_filter(): This function is used to add a filter to a WordPress function. Filters allow you to modify the output of a function before it’s displayed.

function add_dot_to_categories($categories) {
    // Check if $categories is a string (returned by default)
    if (is_string($categories)) {
      // Convert the string to an array by splitting on the comma separator
      $categories = explode(', ', $categories);
    }
    $separator = ' ,  ';
    $output = '';
    if ($categories) {
      foreach ($categories as $category) {
        $output .=  $category . $separator;
      }
      $output = rtrim($output, $separator);
      $output .= ' .';
    }
    return $output;
  }
  add_filter('the_category', 'add_dot_to_categories');

 

Conclusion

Using the provided code, you can easily customize the appearance of category lists on single post pages in WordPress. This code is flexible and can be extended to make custom modifications to category lists.

**

Note: **

Place this code in the functions.php file of your WordPress theme to apply it throughout your entire site.

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

Download

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,102