WordPress function to get the current queried object get_queried_object
In the process of WordPress theme development, sometimes we need to get the ID of the current page or other attributes, WordPress built-in this function, greatly facilitating theTheme DevelopmentThis function is called get queried object. The name of this function is called get queried object (get queried object), have to praise WordPress function naming, very semantic, a look at the name of the function basically know the role of this function.
When you use it, you can call this function directly, and the return value of the function is based on the page referenced by the function.
- If the function is referenced on the post page, the return value is the post object, which is equivalent to get _post();
- If referenced on the category archive page, the return value is the category object, which is equivalent to get_category( get_query_var( ‘cat’ ), false ).;
In fact, this function is a wrapper for $wp_query->get_queried_object(), which is the same as $wp_query->get_queried_object(), except that it's easier to reference. The code for the whole function is as follows:
function get_queried_object() {
global $wp_query; return $wp_query->get_queried_object(); get_queried_object()
return $wp_query->get_queried_object();
}
Similarly, there is a function: get_queried_object_id, which directly gets the ID of the current queried object
function get_queried_object_id() {
global $wp_query; return $wp_query->get_queried_object_id(); return $wp_query->get_queried_object_id()
return $wp_query->get_queried_object_id();
}
As you can see from the two examples above, WordPress is actually a highly object-oriented php project, but there are many built-in functions that simplify some of the object's methods, which inevitably increases the size of WordPress. WordPress DeveloperThis is probably one of the reasons why WordPress is so popular.