Obtener el ID de categoría actual de la página activa


Buscando extraer el ID de categoría de una página específica en WordPress que está listando todas las publicaciones que usan esa categoría específica. Probé lo siguiente, pero no funciona. Puedo obtener el nombre de la categoría usando single_term_title.

$category = single_term_title("", false);
$catid = get_cat_ID( $category );

$category muestra "Entretenimiento", por ejemplo. Pero también necesito la identificación de "Entretenimiento". ¿Cómo lo haría?

Author: RonnieT, 2012-01-12

9 answers

Puede intentar usar get_the_category():

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
 50
Author: ash108,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-24 13:47:14

Si es una página de categoría, puede obtener el id de la categoría actual mediante:

$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;

Si desea obtener el id de categoría de cualquier categoría en particular en cualquier página, intente usar:

$category_id = get_cat_ID('Category Name');
 167
Author: Ram Mehar Deswal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-04 15:25:26

La forma más antigua pero más rápida que puedes usar es:

$cat_id = get_query_var('cat');
 27
Author: Fred K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-18 08:05:05

Uso la función get_queried_object para obtener la categoría actual en una categoría.página de plantilla php.

$current_category = get_queried_object();

Jordan Eldredge tiene razón, get_the_category no es adecuado aquí.

 16
Author: Shaffe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-10 11:00:28

Creo que algunas de las anteriores pueden funcionar, pero el uso de la función get_the_category parece complicado y puede dar resultados inesperados.

Creo que la forma más directa y sencilla de acceder al ID de cat en una página de categoría es:

$wp_query->query_vars['cat']

Salud

 1
Author: Jorge Orpinel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-04 15:25:35

Usé esto para los breadcrums en la página de plantilla de categoría:

$cat_obj = $wp_query->get_queried_object();
$thiscat_id = $cat_obj->term_id;
$thiscat = get_category($thiscat_id);
$parentcat = get_category($thiscat->parent);
 1
Author: guido _nhcol.com.br_,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-09 17:02:49

Alternativa -

 $catID = the_category_ID($echo=false);

EDITAR: La función anterior está obsoleta por favor use get_the_category()

 1
Author: Bheru Lal Lohar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-19 06:38:05

Encontré esta pregunta mientras buscaba exactamente lo que me pediste. Lamentablemente ha aceptado una respuesta incorrecta. Por el bien de otras personas que están tratando de lograr lo que estábamos tratando de lograr, pensé en publicar la respuesta correcta.

$cur_cat = get_cat_ID( single_cat_title("",false) );

Como dijiste single_term_title("", false); estaba devolviendo correctamente el título de la categoría, no estoy seguro de por qué habrías tenido problemas con tu código; pero el código anterior funciona perfectamente para mí.

 0
Author: Bill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-21 14:16:47
$cats = wp_get_post_terms( $post->ID, 'product_cat' );
foreach($cats as $cat){
/*check for category having parent or not except category id=1 which is wordpress default category (Uncategorized)*/
  if($cat->parent != '0' && $cat->term_id != 1){
    echo '<h2 class="link"><a href="'.get_category_link($cat->term_id ).'">'.$cat->name.'</a></h2>';
    break;
  }
}
 0
Author: Abhijit Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-10 04:43:21