如何从自定义类别中获取ID?

时间:2012-12-05 作者:jochem

如何从用户正在浏览的当前自定义分类法(类别)中获取ID?

它使用一种称为“产品”的自定义帖子类型,这些类别称为“pcategories”

对于普通类别,我会得到如下结果:

<?php if ( is_category() )  
{  
    $cat= get_category( get_query_var( \'cat\' ) );
    $cat_id = $cat->cat_ID;
    $categorytable = myrp_api_comparison_table( $category = $cat_id, $number = null, $visit_text = "Visit Text", $image_set = null, $custom_fields = null, $rating_categories = null, $return = false );
    if(empty($categorytable))
    {} else 
    {           
    echo $categorytable;
    } 
} 
?>
那么,如何使用自定义分类法来实现这一点呢?

如果我这样做:

<pre><?php print_r($wp_query->query_vars); ?></pre>

我得到:

Array
(
[pcategory] => televisies
[error] => 
[m] => 0
[p] => 0
[post_parent] => 
[subpost] => 
[subpost_id] => 
[attachment] => 
[attachment_id] => 0
[name] => 
[static] => 
[pagename] => 
[page_id] => 0
[second] => 
[minute] => 
[hour] => 
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] => 
[tag] => 
[cat] => 
[tag_id] => 
[author_name] => 
[feed] => 
[tb] => 
[paged] => 0
[comments_popup] => 
[meta_key] => 
[meta_value] => 
[preview] => 
[s] => 
[sentence] => 
[fields] => 
[category__in] => Array
    (
    )

[category__not_in] => Array
    (
    )

[category__and] => Array
    (
    )

[post__in] => Array
    (
    )

[post__not_in] => Array
    (
    )

[tag__in] => Array
    (
    )

[tag__not_in] => Array
    (
    )

[tag__and] => Array
    (
    )

[tag_slug__in] => Array
    (
    )

[tag_slug__and] => Array
    (
    )

[ignore_sticky_posts] => 
[suppress_filters] => 
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] => 
[posts_per_page] => 10
[nopaging] => 
[comments_per_page] => 50
[no_found_rows] => 
[taxonomy] => pcategory
[term] => televisies
[order] => DESC

1 个回复
最合适的回答,由SO网友:jochem 整理而成

我使用以下代码修复了它:

$current_term = $wp_query->get_queried_object();    
$termid = $current_term->term_id;
效果很好

结束

相关推荐

Page for custom taxonomy

我是WordPress的新手,我刚刚在一个名为arts的自定义帖子类型下创建了一个名为categories的新自定义分类法。有没有办法创建一个自定义页面来显示“类别”的自定义类型?这样,用户可以导航到艺术/类别,并查看类别分类法下的所有术语。