飞龙博客

妙法莲华经

how to get category id for wordpress

feilong.org 修订于2007-06-15 11:44:56

<!--how to get category id for wordpress?Here feilong list three code snippets for you to test.  if a post belongs to 2 categories:
One if 'feilong photos',the id is 12.
The other is 'feilong life',the id is 45.  -->

<!-- 测试区 test area-->
<!--get current urlpage id ,commonly used on achieve templates,very useful~ the result is:
When you are on category-12.php,then it shows 12.
When you are on category-45.php then it shows 45. -->
<?php
$cat= single_cat_title('', false);
echo get_cat_ID($cat);
?>

<!--commonly get one of the post/page categories'ids. 0 to get the first category 12. 1 to get the second category 45.IF on an achieve template,get the first post/page's one.-->
<?php $category = get_the_category();
echo $category[1]->cat_name;
?>
<!--get all of the post/page categories'ids,that is 12 AND 45.IF on an achieve template,get the first post/page's ones.-->
<?php foreach((get_the_category()) as $category) {
echo $category->cat_ID . ' ';
echo $category->cat_name . ' ';
}
?>
<!-- get the first category id directly. -->
<?php $category = get_the_category();
$catid = $category[0]->cat_ID;
echo $catid;?>
<!-- get the first category id by the category name. -->
<?php
$category = get_the_category();
$catname = $category[0]->cat_name;
echo $catname;
$catid = get_cat_ID( $catname );
echo $catid;
?>

<!-- /测试区 /test area-->

更新网址:https://feilong.org/get-category-id-wordpres
最初发布:20070615 11:44:56 feilong.org 于广州

加入收藏夹,查看更方便。

所在分类: wordpress

新作:

旧文: