我在WordPress主题文件夹中创建了一个taxonomy.php页面。我想获得函数的当前项id。我怎么能拿到这个?
get_query_var('taxonomy')
只返回术语slug,我想要ID
永不终止!我找到了:)
get_queried_object()->term_id;
简单易用!
get_queried_object_id()
以下是所需的整个代码片段:
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
使用以下代码
这将打印您当前的分类名称和描述(可选)
<?php
$tax = $wp_query->get_queried_object();
echo ''. $tax->name . '';
echo "<br>";
echo ''. $tax->description .'';
?>
如果您在分类页面中。
这就是获取有关分类法的所有详细信息的方法。
get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
这就是获取分类id 的方法
$termId = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id;
但如果你在后页(紫杉醇->儿童)
$terms = wp_get_object_terms( get_queried_object_id(), 'taxonomy-name');
$term_id = $terms[0]->term_id;
<?php
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
请参阅wp_get_post_terms(),您可以这样做:
global $post;
$terms = wp_get_post_terms( $post->ID, 'YOUR_TAXONOMY_NAME',array('fields' => 'ids') );
print_r($terms);
这是您想要的术语slug。如果你需要的话,看起来你可以得到这样的id:
function get_term_link( $term, $taxonomy = '' ) {
global $wp_rewrite;
if ( !is_object($term) ) {
if ( is_int( $term ) ) {
$term = get_term( $term, $taxonomy );
} else {
$term = get_term_by( 'slug', $term, $taxonomy );
}
}
这适用于所有页面类型,而不仅仅是分类术语档案
$category_id = get_the_category( get_the_ID());
$id = get_category($category_id[0]->term_id);