admin管理员组文章数量:1277297
I'm trying to figure out how to customize the attribute page (for example, if i click on a link to the "length" attribute with a term of "7 inches", wordpress will echo all products that have a length attribute with term 7. My question is, how do I customize such a page?
Hope this is clear.
Thanks!
I'm trying to figure out how to customize the attribute page (for example, if i click on a link to the "length" attribute with a term of "7 inches", wordpress will echo all products that have a length attribute with term 7. My question is, how do I customize such a page?
Hope this is clear.
Thanks!
Share Improve this question asked Apr 4, 2019 at 3:18 TimothyKATimothyKA 292 silver badges5 bronze badges3 Answers
Reset to default 3@TimothyKA your solution might return true when you visit an attribute page however, it will return true on any taxonomy page ( and possibly it will produce PHP warnings on all the rest ).
You need to create a conditional function that returns true only when you are working on an attribute page. The following should work just fine:
function my_is_wc_attribute() {
/**
* Attributes are proper taxonomies, therefore first thing is
* to check if we are on a taxonomy page using the is_tax().
* Also, a further check if the taxonomy_is_product_attribute
* function exists is necessary, in order to ensure that this
* function does not produce fatal errors when the WooCommerce
* is not activated
*/
if ( is_tax() && function_exists( 'taxonomy_is_product_attribute') ) {
// now we know for sure that the queried object is a taxonomy
$tax_obj = get_queried_object();
return taxonomy_is_product_attribute( $tax_obj->taxonomy );
}
return false;
}
After adding the above function to your functions.php, you may create as many filters and actions to customize your attribute pages. For example, the following filter allows you to manipulate only the attributes title:
function my_single_term_title_filter( $attribute_title ) {
if ( my_is_wc_attribute() ) {
// do your stuff here
}
return $attribute_title;
}
add_filter( 'single_term_title', 'my_single_term_title_filter' );
Don't forget to replace the my_ prefixes with your own prefix, to avoid function naming conflicts.
There is a WooCommerce function for that:
is_product_taxonomy(); // Returns true when viewing a product taxonomy archive.
I have found the solution. (code below + explanation for anyone who will perhaps stumble upon this).
$q_object = get_queried_object();
$taxonomy = $q_object->taxonomy;
// if page has taxonomy
if ( is_tax( $taxonomy ) ) {
echo 'THIS WORKS';
} else {
// does not
}
1. get_queried_object();
-- is used on a currently queried object such as single post, archive page, taxonomy page etc... (link to wp codex)
2. $q_object->taxonomy;
-- This allows us to get the taxonomy of the queried object (see point 1.)
3. if ( is_tax( $taxonomy ) ) {
-- Checks if page has taxonomy.
I hope my solution is clear, this is my first time trying to answer my own question and any question on stack-exchange for that matter.
本文标签: Check if page is a woocommerce attribute
版权声明:本文标题:Check if page is a woocommerce attribute 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741215373a2359941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论