


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
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); } }
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:
<?php echo wt_get_category_count(); ?>
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:
<?php echo wt_get_category_count(1); ?>
3. If the alias of the category is provided, the number of category articles corresponding to the abbreviation (alias) will be returned:
<?php echo wt_get_category_count('hello-world'); ?>
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:
<?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=')); ?>
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()
<?php // 将以下category-name改成你的分类别名即可 echo get_category_by_slug('category-name')->count; ?>
Method 4:
Use WordPress built-in function get_category
<?php // 将以下cat_ID改成你的分类ID即可 echo get_category(cat_ID)->count; ?>
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; }
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:
<?php echo 'ID为123的分类及其子孙分类的文章数量为:' . ludou_get_cat_postcount(123); ?>
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >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.

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).

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.

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.

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.
