我有一个简单的条件语句来检查woocommerce / wordpress中的某个产品父类别,但即使值为真(对我来说)它也不起作用。有什么想法吗?
function get_parent_terms($term) {
if ($term->parent > 0) {
$term = get_term_by("id", $term->parent, "product_cat");
if ($term->parent > 0) {
get_parent_terms($term);
} else return $term;
}
else return $term;
}
# Will return all categories of a product, including parent categories
function all_cat_classes($post) {
$cats = "";
$terms = get_the_terms($post->ID, "product_cat");
$key = 0;
// foreach product_cat get main top product_cat
foreach ($terms as $cat) {
$cat = get_parent_terms($cat);
$cats .= (strpos($cats, $cat->slug) === false ? $cat->slug." " : "");
$key++;
}
return $cats;
}
然后此代码最终为false并回显nope: causes
$damaincat = all_cat_classes($post);
if($damaincat == 'causes') echo '<style>#text-3 { display: none !important; }</style>'; else echo 'nope: '.$damaincat;
我被绊倒= 0(