获取活动页面的当前类别 ID [英] Get current category ID of the active page

查看:26
本文介绍了获取活动页面的当前类别 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在 WordPress 中提取特定页面的类别 ID,该页面列出了使用该特定类别的所有帖子.尝试了以下但不起作用.我可以使用 single_term_title 获取类别名称.

Looking to pull the category ID of a specific page in WordPress that is listing all posts using that specific category. Tried the below but not working. I am able to get the category name using single_term_title.

$category = single_term_title("", false);
$catid = get_cat_ID( $category );

$category 例如显示娱乐".但我也需要娱乐"的ID.我该怎么办?

$category is displaying "Entertainment" for example. But I also need the ID of "Entertainment". How would I go about this?

推荐答案

您可以尝试使用 get_the_category():

You can try using get_the_category():

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

这篇关于获取活动页面的当前类别 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆