我有一个简单的条件语句来检查 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;
}
然后这段代码最终是错误的并回显nope: causes
$damaincat = all_cat_classes($post);
if($damaincat == 'causes') echo '<style>#text-3 { display: none !important; }</style>'; else echo 'nope: '.$damaincat;
我被难住了=0(