我試圖獲取正在查看的類別頁面的當前ID。WordPress獲取當前類別ID
我測試過the_category_ID
但這回顯的我的結果,當我用
<?php $catID = the_category_ID(); ?>
有沒有辦法得到它的值返回給變量,以便其隱藏?
我試圖獲取正在查看的類別頁面的當前ID。WordPress獲取當前類別ID
我測試過the_category_ID
但這回顯的我的結果,當我用
<?php $catID = the_category_ID(); ?>
有沒有辦法得到它的值返回給變量,以便其隱藏?
這寫的,而不是附和
<?php $catID = the_category_ID($echo=false);?>
請嘗試以下
$catID = get_query_var('cat');
當前類別ID是全球$cat
變量的變量,當你在一個類別頁面。
你可以測試一下:
<?php echo "Current Category ID is: " . $cat ;?>
當你在例如,此網頁http://example.com/category/test
呃...那很容易。謝謝;) – 2017-06-23 08:21:14
the_category_ID
是deprecated in 2003。
試試這個:
if (is_category()) {
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
}
功能the_category_ID
已被棄用。您需要使用get_the_category()
函數。例如:
$category = get_the_category();
echo $category[0]->cat_name;
多見於WordPress的法典:get_the_category
我試了很多代碼,代碼很成功。如果wan獲得Cat-ID,則使用 - > cat_ID更改 - > cat_name。感謝 – 2016-10-14 15:19:07
$category= get_queried_object();
echo $category->term_id;
這是與其他人的答案不同的方法。謹慎添加一些解釋?另外,在您的帖子中放置代碼時使用代碼塊。 – James 2015-08-07 18:00:38
您將獲得在可變電流類別ID,
<?php $catID = the_category_ID($echo);?>
這不能直接打印,只要給予打印statment那個時候只打印。
函數已棄用。 – addedlovely 2016-06-01 16:34:43
此代碼當前獲得類別ID:
<?php
$category = get_the_category();
echo $category[0]->cat_ID;
?>
它的工作對我來說,今天10月18日2016年
功能已被棄用。 – addedlovely 2016-06-01 16:34:32