如果你是not in the loop 要获取查询的术语,则有原生API函数以及WP_Query
或WP_Tax_Query
对象方法可用(例如get_queried_object()
). 您必须直接访问它。
示例tax_query
看起来(由OP提供):
["tax_query"]=>
object(WP_Tax_Query)#282 (2) {
["queries"]=>
array(2) {
[0]=>
array(5) {
["taxonomy"]=>
string(8) "category"
["terms"]=>
array(1) {
[0]=>
string(5) "alvor"
}
["include_children"]=>
bool(true)
["field"]=>
string(4) "slug"
["operator"]=>
string(2) "IN"
}
[1]=>
array(5) {
["taxonomy"]=>
string(8) "category"
现在我们需要访问它
$wp_query
对象我们将使用一个小插件来实现这一点,该插件将输出必要的部分。
<?php
defined( \'ABSPATH\' ) or exit;
/* Plugin Name: (#90230) Get Taxonomy terms */
add_filter( \'parse_query\', \'wpse90230_parse_query\' );
function wpse90230_parse_query( $wp_query )
{
// This is the tax_query/WP_Tax_Query object
$tax_query = $wp_query->tax_query;
// Now we get the relation `AND`/`OR` so we have a possibilty to the tell the user
// whether we are showing them a "match" or a "filtered" result
$relation = \'AND\' === $tax_query->relation ? "filtered" : "matched";
// Then we\'re extracting the terms.
// This gives us the terms as array sorted by taxonomy.
foreach ( $tax_query->queries as $tax_query )
$terms[ $tax_query["taxonomy"] ] = $tax_query["terms"];
// Now we can loop through them:
printf( \'You are viewing %s\', get_post_type() );
foreach ( $terms as $taxonomy => $terms )
printf(
\'%s: %s\'
,get_taxonomy( $taxonomy )->label
,join( ",", $terms )
);
}