WordPress gets a summary of the number of specified categories of articles

Source: Internet
Author: User
Tags aliases php code

Sometimes we want to get a category (the number of articles under 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:

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

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:

The code is as follows Copy Code
<?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:

The code is as follows Copy Code
<?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:

The code is as follows Copy Code
<?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:

The code is as follows Copy Code

<?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, change to the category ID of the number you want to count, and the final output is the category name (the number of articles), such as the Tutorial Guide (20).


Method Three:
Using the WordPress built-in function Get_category_by_slug ()

The code is as follows Copy Code
<?php
Change the following category-name to your category alias
echo get_category_by_slug (' Category-name ')->count;
?>


Method Four:
Using the WordPress built-in function get_category

  code is as follows copy code

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

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.