WordPress obtains the number of articles under a specific category.

Source: Internet
Author: User
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 the functions under the topic directory. in php: function & nbsp; wt_get_category_count ($ inpu

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:

 

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:

 

3. if the alias of a category is provided, the number of classified articles corresponding to the abbreviated name (alias) is returned:

 

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:

 

After the equal sign 3 of the include parameter is changed to the Category ID of the number of articles you want to count. The final output format is?Category name (number of articles)?, Such?Tutorial Guide (20)

Method 3:

Use the WordPress built-in function get_category_by_slug ()

 Count;?>
Method 4:

Use the WordPress built-in function get_category

 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.

Reference:

  • Get_category_count ()
  • Template Tags/wp list categories
  • Function Reference/get category by slug
  • Function Reference/get category

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.