<?php
$current_term = $wp_query->queried_object->name;
$current_term_id = $wp_query->queried_object->ID;
?>
<hgroup class="section-heading wrapper">
<h1><?php echo $current_term; ?></h1>
<h3><?php echo category_description( $current_term_id ); ?></h3>
</hgroup>
This works fine but the $current_term_id
variable throws a warning â¦
Notice: Undefined property: stdClass::$ID in /Users/my/htdocs/wr/wp-content/themes/wr/taxonomy-event_type.php on line 12
Any ideas what I’m doing wrong here or what’s the real property for the ID of the current term?
I faced the exact same problem for days and then found this!
Use the function
$wp_query->get_queried_object_id()
. Check this for more details.There’s also a wrapper for this function
get_queried_object_id()
. It calls all parts likeglobal $wp_query
by itself.API
The definition of API by the (current) wikipedia article is like the following
This means, that it was written to communicate with core internals and your theme or plugin.
Why?
Because of one easy reason: A wrapping API allows cores internals to transform, while still staying compatible with whatever you throw in as arguments. A wrapper function can add backwards compatibility where a core class/object may not.
It also shortens your code, makes it more readable and allows you to focus on what needs to be done by your code.
When you are on a taxonomy archive (
is_tax()
istrue
)queried_object
is populated in this way (example from my site):So you directly get both name and description. The code you can use is:
$wp_query->queried_object->ID
has several potential points of failure:WP_Post
object. Thequeried_object
can also reference a taxonomy in some circumstances.queried_object
could be null, and thus you’re asking for a property on an nonexistent object.You could be using
$wp_query
in code that does not have a reference to that global. If you determine that$wp_query
itself is null, you should add… somewhere above your code.