Retrieving and Displaying a Complete List of WordPress Site Categories

31

July 30, 2024

August 27, 2024

00:00:00

Today:Saturday

2024/06/05

  • home
  • Wordpress
  • Retrieving and Displaying a Complete List of WordPress Site Categories

Estimated reading time: 5 minutes


Font Size

Excerpt from this article:

The presented code generates an ordered list of categories from a WordPress website, along with links to each category. It utilizes WordPress’s built-in functions to retrieve categories, sort them alphabetically by name, and then display them as a linked list. Subsequently, the significant functions employed in this code and their functionalities will be elaborated upon.

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

 

Code Analysis and Function Explanations:

get_categories():**
This built-in WordPress function retrieves all existing categories on the website and stores them as an array of objects in the variable $categories. Each object within this array contains information such as the category’s identifier, name, parent, and more.

**

usort($categories, function ($a, $b) { ... }):**
● The usort() function is utilized for custom sorting of PHP arrays. In this specific instance, it’s employed to sort the $categories array in ascending order based on the category names.

**● The anonymous function supplied as the second parameter to usort() carries out comparisons between two array elements. This function determines which category should be positioned lower in the order by comparing their names.

**

get_category_link($category->term_id): **
This function returns the URL of the primary page associated with a specific category. It employs the category’s identifier (stored in $category->term_id) and assigns the resulting URL to the $category_link variable.

**

implode(', ', $category_names):**
The implode() function transforms an array into a string, inserting a specified separator between each element. In this particular case, it’s used to convert the $category_names array into a string where category names are separated by commas.

**

array_pop($category_names):**
This function removes and returns the final element of the $category_names array. In this code, it’s done to display the last category’s name independently, followed by a period after the other names.

<div class="SC_category-list">
  <?php
  $categories = get_categories();
  usort($categories, function ($a, $b) {
    return strcasecmp($a->name, $b->name);
  });
  $category_names = array();
  foreach ($categories as $category) {
    $category_link = get_category_link($category->term_id);
    $category_name = $category->name;
    $category_names[] = '<a href="' . $category_link . '">' . $category_name . '</a>';
  }
  $last_category_name = $category_names[count($category_names) - 1]; // Get the last category name
  array_pop($category_names); // Remove the last category name from the array
  $comma_separated_names = implode(', ', $category_names); // Join category names with commas
  echo $comma_separated_names . ', ' . $last_category_name . '.'; // Add space and last category name
  ?>
</div>

 

Overall Code Functionality:

1- Initially, all categories from the website are fetched.
**2- Categories are arranged in ascending order based on their names.
**3- For each category, the corresponding link is generated, and the category name along with the link is saved in a new array.
**4- The last element of the newly created array is processed individually to separate it with a period.
**5- The remaining array elements are separated by commas, and finally, the last category name is appended to the resulting string.

 

Outcome:

The output of this code is an ordered list of category links that can be incorporated into any part of the WordPress template.

 

 

Applications:

Displaying a list of categories in the site’s sidebar. Displaying a list of related categories below each post. Creating an archive page for all categories.

 

Note:**
● To modify the visual appearance of the output, you can adjust the HTML formatting.
**● To introduce additional features, such as displaying the number of posts within each category, you can expand the code.

**

By comprehending these explanations, you can customize this code for your WordPress projects and utilize it to showcase your site’s categories.

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

6,735