wordpress通过分类名获取分类ID函数:get_cat_ID()


【函数介绍】

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

【函数用法】

  1. <?php get_cat_ID( $cat_name ) ?>

【参数】

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

【返回的值】

(整数)

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

【示例】

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

  1. <?php
  2. $category_id = get_cat_id('Category Name');
  3. $q = 'cat=' . $category_id;
  4. query_posts($q);
  5. if (have_posts()) : while (have_posts()) : the_post();
  6. the_content();
  7. endwhile; endif;
  8. ?>

【源代码】

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