WordPress Gets the number of articles under the specified category and its subcategories, the number of WordPress articles _php Tutorial

Source: Internet
Author: User

WordPress Gets the number of articles under the specified category and its subcategories, number of WordPress articles


Get the number of specific category articles

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

Method One:

Place the following PHP code in the functions.php in 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 call it:

1, the function is paged out 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, the supplied parameter is a number, and the number is the ID number of the classification, then returns the number of articles that correspond to the category of the ID:

<?php echo wt_get_category_count (1);?>

3, provide the alias of the classification, then return the corresponding thumbnail name (alias) of the number of classification articles:

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

This function has a slight error in the number of articles in the classification of sub-classifications. Statistics are also not very good for the number of classification articles 0.

Method Two:

In fact, we can directly use the built-in function of WordPress wp_list_categories (), just transfer the function when the note on 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= '));?>

Parameter include after 3 change to the number you want to count the category ID can be, the final output form is the category name (article number)

Method Three:

Using WordPress built-in Functions 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 WordPress built-in functions 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 get to the simple number of articles, in terms of code, method one code is the most, method three or four code is the least. In terms of execution efficiency, the method has an execution time of about 0.002 seconds, the highest efficiency, the method four times, the execution time is about 0.004 seconds, the method three is the worst, the execution time is about 0.008 seconds. The reason why there is such a big difference in execution efficiency, because method one is focused on a thing, is to find the number of articles, only one database query, and method three and method four is the 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, It is used to get all the information about the classification! In addition, none of the three methods count the number of articles under subcategories.

All of the above methods do not have a good or bad point, the execution time of a few milliseconds is not felt at all, you can choose according to personal preferences related methods.

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

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

function Ludou_get_cat_postcount ($id) {//Gets the current classification information $cat = get_category ($id);/////Current category article number $count = (int) $cat->count;// Gets the current classification of all descendant categories $tax _terms = get_terms (' category ', Array (' child_of ' = ' = $id)); foreach ($tax _terms as $tax _term) {  //descendants classified article count cumulative  $count + = $tax _term->count;} return $count;}

Using the example

Well, the function definition is complete, just pass the class ID parameter to the Ludou_get_cat_postcount function when you use it, here is the example:

<?php Echo ' ID 123 category and its descendants classified the number of articles as: '. Ludou_get_cat_postcount (123);? >

Articles you may be interested in:

    • Functions used in WordPress for obtaining information about articles and classifying links
    • The method to get the author and classified information of the article in WordPress

http://www.bkjia.com/PHPjc/1087271.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087271.html techarticle get the number of articles under the specified classification and its subcategories in WordPress, the number of WordPress articles gets the number of specific categories of articles sometimes we want to get the number of articles under a category (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.