search
HomeBackend DevelopmentPHP TutorialGet 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:

<&#63;php echo wt_get_category_count(); &#63;>

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;>

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;>

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;>

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;>

Method 4:

Use WordPress built-in function get_category

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

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:

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

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
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
wordpress后台乱码怎么办wordpress后台乱码怎么办Feb 03, 2023 pm 01:48 PM

wordpress后台乱码的解决办法:1、在wordpress的“wp-admin”文件夹下找到“admin.header.php”文件;2、将“charset”属性值设置为“UTF-8”格式即可恢复正常。

如何解决wordpress标签错误问题如何解决wordpress标签错误问题Feb 03, 2023 pm 02:03 PM

wordpress标签错误的解决办法:1、找到并打开wordpress的“wp-includes”目录下的“class-wp.php”文件;2、修改内容为“$pathinfo = isset( $_SERVER['PATH_INFO'] )?mb_convert_encoding($_SERVER['PATH_INFO'],'utf-8','GBK') : '';”即可。

WordPress设置独立的Description和KeywordsWordPress设置独立的Description和KeywordsFeb 21, 2023 am 11:14 AM

你下载的WordPress主题提供的keywords和description这两个meta标签一般都做得很差,或者根本就不提供,这样不利于SEO。本文将指导你如何给主页、分类、页面以及文章页添加单独的Description 和 Keywords。

wordpress乱码怎么办wordpress乱码怎么办Mar 09, 2023 am 09:13 AM

wordpress乱码的解决办法:1、修改“wp-config.php”文件里的“define(’DB_CHARSET’, ‘utf8′);”为“define(’DB_CHARSET’, ”);”;2、把新数据库的编码设置成“latin1_swedish_ci”;3、以uft8的格式导入备份的数据库文件即可。

wordpress进不去怎么办wordpress进不去怎么办Feb 23, 2023 am 09:41 AM

wordpress进不去的解决办法:1、把地址栏“wp-login.php”后面的参数删掉,然后重新输入密码登录;2、登录FTP,下载“pluggable.php”文件,然后找到“ADMIN_COOKIE_PATH”并将它替换为“SITECOOKIEPATH”即可。

wordpress是saas吗wordpress是saas吗Feb 21, 2023 am 10:40 AM

wordpress不是saas。SaaS是一种软件销售模式,它主要针对云端应用软件,而WordPress是一款CMS系统,它主要针对网站构建和管理。虽然WordPress可以作为SaaS提供服务,但它本质上不是一种SaaS应用。

2023年最新WordPress视频教程推荐2023年最新WordPress视频教程推荐Oct 25, 2019 pm 01:12 PM

本次PHP中文网整合了相关的视频教程,中文手册,以及相关的精选文章安利给大家,统统免费!!!通过我们分享的视频,可随时随地免费观看教程视频,也不需要迅雷或者百度网盘下载了。

wordpress是哪一年的wordpress是哪一年的Feb 01, 2023 am 10:26 AM

wordpress是2003年发布的;Matt于2003年5月27日宣布推出第一版WordPress,受到了社区的欢迎,它基于b2 Cafelog并有显著改进;WordPress的第一个版本包括全新的管理界面、模板、XHTML 1.1兼容模板、内容编辑器。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool