¿Está buscando una manera de contar todas las entradas de una categoría y mostrarlo en WordPress? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que se puede utilizar para contar todos los mensajes en una categoría en WordPress.
Instrucciones:
Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:
function count_cat_post($category) { if(is_string($category)) { $catID = get_cat_ID($category); } elseif(is_numeric($category)) { $catID = $category; } else { return 0; } $cat = get_category($catID); return $cat->count; }
Puede utilizar este código para mostrar el recuento de entradas de una categoría en cualquier lugar de su sitio web, ya sea por ID de categoría o por nombre de categoría:
<?php // echo count_cat_post('1'); // echo count_cat_post('General'); ?>
Simplemente elimine ‘//’ de cualquiera de las líneas de código para utilizarlo.
Nota: Si es la primera vez que añades fragmentos de código en WordPress, consulta nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente tu sitio.
Si te ha gustado este fragmento de código, por favor considere revisar nuestros otros artículos en el sitio como: 15 mejores herramientas de marketing de contenidos y cómo crear un formulario de contacto en WordPress.
Hi,
How can i use the same function but with the ‘AND’ relation.
only posts that have both categories will return.
How can we pass the id or name dynamically?
You can pass a variable to the function. For example, you could pass the first returned category id of the
get_the_category()
function: https://developer.wordpress.org/reference/functions/get_the_category/Note that this requires working with arrays and objects, as the returned value from
get_the_category()
is an array of objects.For example, you could use:
$cat = get_the_category();
$cat_id = $cat[0]->cat_ID;//Gets the first id
With the latest version of WP, there were some bugs with all of this working right out of the box, so if anyone else has trouble:
function count_cat_post($catslug){
$CategoryTerms = get_term_by('slug', $catslug, 'category');
if(!$CategoryTerms){
return 0;
}
return $CategoryTerms->count;
}
Hi Jason,
Thank you for this. However, we were able to test on WordPress 5.4 and it is working for us without errors.
ALSO,
use get_the_category since get_category will error out with the latest versions of wordpress on anything that has posts and return 0 on the ones that don’t, causing a little bit of a troubleshooting conundrum.
We are not seeing this. What is the error message you are getting?
also it:
return $cat->category_count;
since count is a php function.
$cat
is aWP_Term
object, withcount
being an public int property.is the any way to not put category name or id? i have so many categories. cant come to code and add each ones id or name in the code. need wordpress to do it automaticly.
In your template there will be an area where it is outputting the category name, there is likely a part of that that uses a variable that is equal to the category id or slug, that you can use instead to pull the count. Something that ends up like:
// echo count_cat_post($cat_id);
You could use a
for
loop to cycle through the categories. You may want to useget_categories()
in this case: https://developer.wordpress.org/reference/functions/get_categories/