WordPress obtains the number of articles under a specified category and its subcategories, and the number of wordpress articles. WordPress obtains the number of articles under a specified category and its subcategories. wordpress obtains the number of articles under a specific category. sometimes we want to obtain the number of articles under a certain category, wordPress obtains the number of articles under a specified category and its subcategories, and the number of wordpress articles.
Obtain the number of articles in a specific category
Sometimes we want to get the number of articles under a category so that they can be displayed somewhere in the blog. The following describes how to obtain the number of articles in a specific category. you can choose one based on your preferences:
Method 1:
Place the following PHP code in functions. php under the topic 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); }}
You can call the function as needed. the Function provides three call methods:
1. if this function is called up in the main loop and no parameters are provided, the number of articles in the first category is returned:
<?php echo wt_get_category_count(); ?>
2. if the provided parameter is a number and the number is the ID of the category, the number of articles with the corresponding ID is returned:
<?php echo wt_get_category_count(1); ?>
3. if the alias of a category is provided, the number of classified articles corresponding to the abbreviated name (alias) is returned:
<?php echo wt_get_category_count('hello-world'); ?>
This function has a slight error in the number of articles for classification containing subcategories. The statistics on the number of classified articles is not very good.
Method 2:
In fact, we can directly use the WordPress built-in function wp_list_categories (), just pay attention to it 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 value 3 after the equal sign of the include parameter to the Category ID of the number of articles you want to collect. The final output form is the category name (number of articles)
Method 3:
Use the WordPress built-in function get_category_by_slug ()
<? Php // change the following category-name to your category alias to echo get_category_by_slug ('category-name')-> count;?>
Method 4:
Use the WordPress built-in function get_category
<? Php // change the following cat_ID to your category ID to echo get_category (cat_ID)-> count;?>
Summary:
Methods 1, 3, and 4 can obtain the number of simple articles. for 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 1 is about 0.002 seconds, with the highest efficiency. Method 4 is the second, and the execution time is about 0.004 seconds. Method 3 is the worst, and the execution time is about 0.008 seconds. The reason for such a big difference in execution efficiency is that method 1 focuses on one thing, that is, to find the number of articles and only execute one database query. Method 3 and method 4 are WordPress built-in functions, although only one line of code is required, they are not designed to query the number of articles in a category, but to obtain all the information about the category! In addition, the three methods do not count the number of articles under the subcategory.
All of the above methods do not have the advantages and disadvantages, and the execution time difference is several milliseconds. you can choose the relevant methods based on your preferences.
Obtains the number of articles for a specified category and its subcategories.
Sometimes we need to get the number of articles for the specified category and all its subcategories. next we will look at the implementation methods.
First, define the implementation function and copy the following php code to functions. php of the current topic:
Function ludou_get_cat_postcount ($ id) {// Obtain the current category information $ cat = get_category ($ id); // Number of articles of the current Category $ count = (int) $ cat-> count; // Obtain all descendant classes of the current category $ tax_terms = get_terms ('Category ', array ('child _ of' => $ id); foreach ($ tax_terms as $ tax_term) {// add the number of articles for the descendant categories $ count + = $ tax_term-> count;} return $ count ;}
Example
Okay. After defining the function, you only need to pass the category id parameter to the ludou_get_cat_postcount function. The following is an example:
<? The number of articles in the php echo 'Id 123 and its descendant categories is: '. ludou_get_cat_postcount (123);?>
Articles you may be interested in:
- Function usage of WordPress for retrieving document information and Classification links
- How to get the author and category information in WordPress
Keyword: get the number of articles in a specific category. sometimes we want to get the number of articles under a certain category ,...