Table of Contents
Get the number of articles under a specified category and its subcategories in WordPress, the number of wordpress articles
Articles you may be interested in:
Home Backend Development PHP Tutorial Get the number of articles under a specified category and its subcategories in WordPress, the number of wordpress articles_PHP tutorial

Get the number of articles under a specified category and its subcategories in WordPress, the number of wordpress articles_PHP tutorial

Jul 12, 2016 am 09:01 AM
wordpress Classification article

Get the number of articles under a specified category and its subcategories in WordPress, the number of wordpress articles

Get the number of articles in a specific category

Sometimes we want to get the number of articles under a certain category so that it can be displayed somewhere on the blog. Here are several ways to get the number of articles in a specific category. You can choose according to your personal preferences:

Method 1:

Place the following PHP code in functions.php in the theme directory:

function wt_get_category_count($input = '') {
 global $wpdb;

 if($input == '') {
  $category = get_the_category();
  return $category[0]->category_count;
 }
 elseif(is_numeric($input)) {
  $SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input";
  return $wpdb->get_var($SQL);
 }
 else {
  $SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
  return $wpdb->get_var($SQL);
 }
}

Copy after login

Then just call the function where needed. This function provides three calling methods:

1. Call this function in the main loop and provide no parameters, then return the number of articles in the first category:

<&#63;php echo wt_get_category_count(); &#63;>
Copy after login

2. The provided parameter is a number, and the number is the ID number of the category, then the number of articles in the category corresponding to the ID is returned:

<&#63;php echo wt_get_category_count(1); &#63;>
Copy after login

3. If the alias of the category is provided, the number of category articles corresponding to the abbreviation (alias) will be returned:

<&#63;php echo wt_get_category_count('hello-world'); &#63;>
Copy after login

This function will have a slight statistical error in the number of articles for categories containing subcategories. Statistics for cases where the number of classified articles is 0 are not very good either.

Method 2:

In fact, we can directly use WordPress’s built-in function wp_list_categories(), just pay attention when passing the function:

<&#63;php echo strip_tags(wp_list_categories('include=3&hide_empty=0&use_desc_for_title =0&echo=0&show_count=1&style=none&hierarchical =0&title_li=')); &#63;>
Copy after login

Change the 3 after the equal sign in the include parameter to the category ID you want to count the number of articles. The final output format is category name (number of articles)

Method 3:

Use WordPress built-in function get_category_by_slug()

<&#63;php
 // 将以下category-name改成你的分类别名即可
 echo get_category_by_slug('category-name')->count; 
&#63;>
Copy after login

Method 4:

Use WordPress built-in function get_category

<&#63;php
 // 将以下cat_ID改成你的分类ID即可
 echo get_category(cat_ID)->count; 
&#63;>
Copy after login

Summary:

Methods 1, 3, and 4 can obtain the pure number of articles. In terms of the amount of code, method 1 has the most code, and method 3 and 4 have the least code. In terms of execution efficiency, the execution time of method one is about 0.002 seconds, which is the most efficient; the execution time of the fourth method is about 0.004 seconds; the execution time of method three is the worst, and the execution time is about 0.008 seconds. The reason why there is such a big difference in execution efficiency is that method one focuses on one thing, which is to find the number of articles, and only performs a database query, while methods three and four are WordPress built-in functions, although they only require one line of code. But they are not specifically designed to query the number of classified articles, but to obtain all the information of the classification! In addition, these three methods will not count the number of articles under subcategories.

There is no distinction between any of the above methods that is better or worse. The few milliseconds difference in execution time cannot be felt at all. You can choose the relevant method according to your personal preferences.

Get the number of articles in the specified category and its subcategories

There may be times when we need to obtain the number of articles in a specified category and all its subcategories. Let’s take a look at the relevant implementation methods.
First, define the implementation function and copy the following php code into functions.php of the current theme:

function ludou_get_cat_postcount($id) {
 // 获取当前分类信息
 $cat = get_category($id);

 // 当前分类文章数
 $count = (int) $cat->count;

 // 获取当前分类所有子孙分类
 $tax_terms = get_terms('category', array('child_of' => $id));

 foreach ($tax_terms as $tax_term) {
  // 子孙分类文章数累加
  $count +=$tax_term->count;
 }
 return $count;
}

Copy after login

Usage examples

Okay, the function is defined. When using it, you only need to pass the classification id parameter to the ludou_get_cat_postcount function. The following is an example of use:

<&#63;php
 echo 'ID为123的分类及其子孙分类的文章数量为:' . ludou_get_cat_postcount(123);
&#63;>
Copy after login

Articles you may be interested in:

  • Usage of functions in WordPress to obtain article information and category links
  • Methods in WordPress to obtain article author and category information Organize

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1087271.htmlTechArticleGet the number of articles under a specified category and its subcategories in WordPress. Sometimes, the number of WordPress articles can be used to get the number of articles in a specific category. We want to get the number of articles under a certain category (category),...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

How to register a wordpress account How to register a wordpress account Apr 20, 2025 am 11:45 AM

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.

How to import wordpress templates How to import wordpress templates Apr 20, 2025 am 10:18 AM

WordPress templates quickly create professional websites. The steps to import a template include: select and download the template. Log in to the WordPress dashboard. Select Theme from the Appearance menu. Click "Add New Theme". Click "Upload topic" and select the downloaded template .zip file. Click "Install Now". Click the "Activate" button. Customize the templates through the Customize menu.

See all articles