飞龙博客

飞龙博客 飞龙在天

how to get category id for wordpress

feilong.org 修订于2007-06-15 11:44:56 282 次浏览

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

新作:

旧文:

blog money bui 教程 echarts 教程 eclipse 教程 html css 教程 IT趋势 js 教程 json 教程 mysql 教程 nodejs npm 教程 onblog SQLite 教程 tutorials vscode 教程 wap网站 winpe启动盘 wordpress 主机推荐 书法 前端开发 博客 名企名网 国学 域名注册 常用软件 建站程序 操作系统 教程 数码 无线上网 日记 电子商务 电脑笔记本 网站 网站优化 网站开发 网站推广 网站策划 网络 虚拟现实 默认