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:

<?php echo wt_get_category_count(); ?>
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:

<?php echo wt_get_category_count(1); ?>
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:

<?php echo wt_get_category_count('hello-world'); ?>
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:

<?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=')); ?>
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()

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

Method 4:

Use WordPress built-in function get_category

<?php
 // 将以下cat_ID改成你的分类ID即可
 echo get_category(cat_ID)->count; 
?>
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:

<?php
 echo 'ID为123的分类及其子孙分类的文章数量为:' . ludou_get_cat_postcount(123);
?>
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 Article Tags

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)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP vs. Flutter: The best choice for mobile development

How to change page width in wordpress How to change page width in wordpress Apr 16, 2024 am 01:03 AM

How to change page width in wordpress

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

In which folder are wordpress articles located?

How to create a product page in wordpress How to create a product page in wordpress Apr 16, 2024 am 12:39 AM

How to create a product page in wordpress

How to search for authors in WordPress How to search for authors in WordPress Apr 16, 2024 am 01:18 AM

How to search for authors in WordPress

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

Where is the wordpress template file?

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

Which version of wordpress is stable?

What language is used to develop WordPress? What language is used to develop WordPress? Apr 16, 2024 am 12:03 AM

What language is used to develop WordPress?

See all articles