WordPress获取当前分类ID的四种方法

WordPress获取当前分类ID的四种方法

时间: 2015-01-05 所属栏目: Wordpress教程 作者: WP管理员之家 关键词: wordpress,分类ID 关注热度: 4,346 次 (1条)

WordPress获取当前分类ID的方法有很多,今天我来给大家总结一下吧,wordpress主题定制专家-WP管理员之家总结下来一共有4种,下面一一为大家道来:

方法一:最简单最直接

如果是分类页,系统默认有个变量$cat,就是当前分类的ID。这个比较准。

方法二:在page单页面使用

  1. $cat= single_cat_title('', false);
  2. echo get_cat_ID($cat);
  3. ?>

方法三:有点小小的复杂

  1.     if (!is_page() && !is_home()){
  2.         $catsy = get_the_category(); $myCat = $catsy[0]->cat_ID; $currentcategory = '¤t_category='.$myCat;
  3.     }
  4.     wp_list_categories('hierarchical=1&use_desc_for_title=0&exclude=12&depth=1&orderby=id&title_li='.$currentcategory);
  5. ?>

方法四:绕远路通过循环来实现

  1.     foreach((get_the_category()) as $category) {
  2.         echo $category->cat_ID . ''; //当前文章的分类的ID
  3.         echo $category->cat_name . ''; //当前文章的分类的名称
  4.     }
  5. ?>

 

你可能感兴趣的:(wordpress)