飞龙博客

feilong.org 前端开发教程

how to get category id for wordpress

飞龙修订于 20070615 11:44 8 次浏览

<!--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

想学前端?长按二维码加我微信

想学前端?点击飞龙QQ

分类: wordpress

新作:

旧文:

关闭

长按二维码 -> 识别图中二维码

打开微信,发现,扫一扫

支付完成后,点击下一步

感谢大力支持

然后你手机号:

最后点这,稍等即可