2013-03-05 166 views

回答

2

這寫的,而不是附和

<?php $catID = the_category_ID($echo=false);?> 
+2

功能已被棄用。 – addedlovely 2016-06-01 16:34:32

6

請嘗試以下

$catID = get_query_var('cat');

40

當前類別ID是全球$cat變量的變量,當你在一個類別頁面。

你可以測試一下:

<?php echo "Current Category ID is: " . $cat ;?> 

當你在例如,此網頁http://example.com/category/test

+1

呃...那很容易。謝謝;) – 2017-06-23 08:21:14

5

the_category_IDdeprecated in 2003

試試這個:

if (is_category()) { 
    $category = get_category(get_query_var('cat')); 
    $cat_id = $category->cat_ID; 
} 
3

功能the_category_ID已被棄用。您需要使用get_the_category()函數。例如:

$category = get_the_category(); 
echo $category[0]->cat_name; 

多見於WordPress的法典:get_the_category

+0

我試了很多代碼,代碼很成功。如果wan獲得Cat-ID,則使用 - > cat_ID更改 - > cat_name。感謝 – 2016-10-14 15:19:07

0
$category= get_queried_object(); 
echo $category->term_id; 
+1

這是與其他人的答案不同的方法。謹慎添加一些解釋?另外,在您的帖子中放置代碼時使用代碼塊。 – James 2015-08-07 18:00:38

-3

您將獲得在可變電流類別ID,

<?php $catID = the_category_ID($echo);?> 

這不能直接打印,只要給予打印statment那個時候只打印。

+0

函數已棄用。 – addedlovely 2016-06-01 16:34:43

2

此代碼當前獲得類別ID:

<?php 
$category = get_the_category(); 
echo $category[0]->cat_ID; 
?> 

它的工作對我來說,今天10月18日2016年