我有一个前端表单,正在使用此方法添加分类和描述:$job_name_description = array( 'cat_name' => $people, 'category_description' => $people_description, 'taxonomy' => 'job'); wp_insert_category( $job_name_description );这工作正常。但是在该代码运行后,我现在需要获取刚刚添加的 term_id。例如,如果在运行该代码后我在数据库中看到了这个.........然后我需要知道 term_id 是什么(在本例中为 38)并将其设为变量。
2 回答

饮歌长啸
TA贡献1951条经验 获得超3个赞
我想到了;
$job_name_description = array(
'cat_name' => $people,
'category_description' => $people_description,
'taxonomy' => 'job'
);
$result = wp_insert_category($job_name_description);
$term_id = $result;
echo 'The term ID is ' . $result;
这是因为,正如@suspectus 指出的, wp_insert_category 返回类别 ID ( http://hookr.io/functions/wp_insert_category/ )。

慕莱坞森
TA贡献1810条经验 获得超4个赞
wp_insert_category()
返回类别 ID。
$cat_id = wp_insert_category( $job_name_description );
然后使用get_term_by()
来获取术语 id:
get_term_by_id('id', $cat_id, 'job');
- 2 回答
- 0 关注
- 304 浏览
添加回答
举报
0/150
提交
取消