WordPress Method for Getting the Number of Specified Category Articles

Time:2019-8-13

This article illustrates how WordPress can get the number of articles in a given category. Share for your reference. The concrete realization method is as follows:

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

Copy code

The code is as follows:

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 it can call the function where it is needed. The function can be called in three ways:

1. If the function is called 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

The code is as follows:

<?php echo wt_get_category_count(); ?>

2. If the parameter provided is a number and the number is the ID number of the classification, the number of articles of the classification corresponding to the ID will be returned.

Copy code

The code is as follows:

<?php echo wt_get_category_count(1); ?>

3. Provide an alias for a classification, and return the number of categories with corresponding abbreviations (aliases):

Copy code

The code is as follows:

<?php echo wt_get_category_count(‘hello-world’); ?>

For classifications containing molecular classifications, there will be slight statistical errors in the number of articles. The statistics on the number of articles categorized as 0 are not very good.
 
Method 2:

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

Copy code

The code is as follows:

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

Change the third after the equal sign of the parameter include to the classification ID of the number of articles you want to count. The final output form is the category name (number of articles), such as the tutorial guide (20).
 
Method 3:

Use WordPress built-in function get_category_by_slug()

Copy code

The code is as follows:

<?php
// Change the following category-name to your category alias
echo get_category_by_slug(‘category-name’)->count;
?>

 
Method 4:

Use WordPress built-in function get_category

Copy code

The code is as follows:

<?php
// Change the following cat_ID to your classification ID
echo get_category(cat_ID)->count;
?>

I hope this article will be helpful to the construction of WordPress.

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]