Is there a more "WordPress way" to do this...
I don't really know what you mean by a more WordPress way, but in general, the way you are getting the term description is a more reliable way in doing it. This way, you do not need to access the $post
global which so many people use. Just for interest sake, please see my post here on why to avoid the $post
global
I would however get the queried object from the $GLOBALS['wp_the_query']->get_queried_object()
which is even more reliable than get_queried_object()
, and then pass that through get_term()
to apply the term filters to the term
} elseif ( is_tax() ) {
//Product categories
$obj_temp = get_term( $GLOBALS['wp_the_query']->get_queried_object() );
$description = $obj_temp->description;
}
Another probable way is to get the term slug and taxonomy from the URL and then get the complete term object to get the description
} elseif ( is_tax() ) {
//Product categories
$obj_temp = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
$description = $obj_temp->description;
}
EDIT - from comments
How is $GLOBALS['wp_the_query']->get_queried_object()
more reliable than get_queried_object()
We actually had a discussion about this which lead to my answer to the following question
You should really read that as I explained everything in detail there. Just a quick proof of concept to add to that answer in this context, in any template, preferably a page or taxonomy template, run the following and see the results
var_dump( $GLOBALS['wp_the_query']->get_queried_object() );
var_dump( get_queried_object() );
query_posts( 's=crap' );
var_dump( $GLOBALS['wp_the_query']->get_queried_object() );
var_dump( get_queried_object() );
Also, what's an undesirable situation that might result if the term filters didn't get applied to the term
Term data is not sanitized, specially when used directly from a super global, so it may contain injected malicious code or hacked URL's to some hardcore porn site which you do not want. Applying the term filters sanitizes the data according to term field to make it save to use.
Sanitizing, validating and escaping data should be a habit, and not a debatable extra. Hackers love non-sanitized and non-validated data, they can inject any malicious code into your site and through that gain access to your site. The best advice I can ever tell you forever is, ALWAYS SANITIZE, VALIDATE AND ESCAPE all data according to data type, regardless of where it comes from (even if it comes from you yourself) or what it will be used for. This way, you make it extremely hard for hackers to hack your site