> WordPress中文手册 > wordpress通过分类名获取分类ID函数:get_cat_ID()

【函数介绍】

get_cat_ID()通过分类名获取分类ID,如果没检索到数据则返回0

【函数用法】

<?PHP get_cat_ID( $cat_name ) ?>

【参数】

$cat_name
(string) (可选) 分类名称,默认为 ‘General’ .
默认: ‘General’

【返回的值】

(整数)

出错时返回0,成功则返回类别编号。

【示例】

以下是在The_Loop(循环)中使用该函数的基本示例。

<?php       
   $category_id = get_cat_id('Category Name');       
   $q = 'cat=' . $category_id;       
   query_posts($q);       
   if (have_posts()) : while (have_posts()) : the_post();         
   the_content();         
   endwhile; endif;  
?>  

【源代码】

get_cat_ID() 位于 wp-includes/category.php中。