WordPress obtains the total number of articles in a specified category (subcategory)

Source: Internet
Author: User

Implementation functions

First, define the implementation function and copy the following php code to functions. php of the current topic:

The code is as follows: Copy code
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 categories of the current category
$ Tax_terms = get_terms ('Category ', array ('child _ of' => $ id ));
Foreach ($ tax_terms as $ tax_term ){
// Add the number of sub-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 code is as follows: Copy code
<? Php
The number of articles in echo 'Id 123 and its descendant categories is: '. ludou_get_cat_postcount (123 );
?>

Obtain the number of articles under a certain category

The code is as follows: Copy code

<? Php echo get_category_by_slug ($ category_nickname)-> count;?>

 
<? Php echo get_category ($ category_ID)-> count;?>

Obtain other categories

The code is as follows: Copy code

<? Php

$ Cat = get_category_by_slug ($ category_nickname );
// $ Cat = get_category ($ category_ID );
Echo $ cat-> term_id; // Obtain the Category id
Echo $ cat-> count; // Obtain the number of articles by category
Echo $ cat-> description; // Obtain the category description.
Echo $ cat-> name; // Obtain the category name
Echo $ cat-> slug; // Obtain the alias of a category
Echo $ cat-> parent; // gets a reference to the parent category of the current category
?>

Obtain the number of articles in a specific category

Place the following PHP code in functions. php under the topic directory:

The code is as follows: Copy code


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

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:

The code is as follows: Copy code


<? 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:

The code is as follows: Copy code


<? 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:

The code is as follows: Copy code


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

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.