WordPress Gets the number of articles in the specified category and its subcategories _php instance

Source: Internet
Author: User
Tags aliases

Get the number of articles in a specific category

Sometimes we want to get the number of articles under a category (category) to show up somewhere in the blog. Here are a few ways to get the number of articles for a particular category, and you can choose from your personal preferences:

Method One:

Place the following PHP code in the 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);
 }


Then call the function where it is needed, which provides three ways to invoke it:

1. When the function is called in the main loop and no arguments are supplied, the number of articles in the first category is returned:

<?php echo Wt_get_category_count ();?>

2, the supplied parameter is a number, and the number is the ID number of the category, then the number of articles for the category of the corresponding ID is returned:

<?php echo wt_get_category_count (1);?>

3, provide the classification of aliases, then return the corresponding abbreviations (aliases) the number of categories of articles:

<?php echo wt_get_category_count (' Hello-world ');?>

In this function, there is a slight error in the number of articles in the classification that contains subcategories. The number of classified articles is 0 is not very good statistics.

Method Two:

In fact, we can directly use the built-in functions of WordPress wp_list_categories (), just transfer the function of the time to pay attention to the line:

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

After the equals sign of the parameter include 3, change to the category ID of the number you want to count, and the final output is the category name (number of articles)

Method Three:

Using 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 Four:

Using the WordPress built-in function get_category

<?php
 //change the following cat_id to your category ID to
 Echo get_category (cat_id)->count; 
? >

Summarize:

Method One, three or four can obtain the pure article quantity, in the code quantity, method one's code is most, method three or four's code is the least. In terms of execution efficiency, the method has an execution time of about 0.002 seconds, the highest efficiency, and four times the execution time is about 0.004 seconds, the worst of which is the execution time of about 0.008 seconds. The reason for this difference is that the method is focused on one thing, that is, to find the number of articles, only to perform a database query, and method three and method four are WordPress built-in functions, although only a single line of code, but they are not specifically for the query classification of the number of articles designed, But to get all the information for the classification! In addition, these three methods do not count the number of articles under the subcategory.

All of the above methods do not have the advantage of the bad, the execution time of the few milliseconds is not felt at all, you can according to personal preferences to choose the relevant methods.

Gets the number of articles for the specified category and its subcategories

There may be times when we want to get the number of articles for the specified category and all of its subcategories, let's look at the relevant implementation.
First, define the implementation function and copy the following PHP code to the functions.php of the current topic:

function Ludou_get_cat_postcount ($id) {
 //Get Current classification information
 $cat = Get_category ($id);

 Current category article number
 $count = (int) $cat->count;

 Get current category All descendants classified
 $tax _terms = get_terms (' category ', Array (' child_of ' => $id));

 foreach ($tax _terms as $tax _term) {
  //descendants category article number cumulative
  $count + + $tax _term->count;
 }
 return $count;
}

Using the sample

OK, the function is defined, you can use the Ludou_get_cat_postcount function to pass the category ID parameter, the following is the use of the example:

The
 number of articles classified by <?php Echo ' ID 123 and its descendants is: '. Ludou_get_cat_postcount (123);
? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.