#php #wordpress #categories
#php #wordpress #Категории
Вопрос:
У меня есть глобальный вызываемый $best_cats
global $best_cats;
$best_cats = array(8,31,51,102,217,218);
Теперь мне нужно проверить, находится ли текущий cat внутри массива, И ЕСЛИ текущий cat, является родительским cat.
Я пытаюсь так, но не работает. Что я делаю не так и как я могу это сделать?
<?php
$this_category = get_category($cat);
$cat_id = $this_category->cat_ID;
if (in_array($cat_id, $best_cats) amp;amp; ($this_category->category_parent == 0 )) { ?>
//category is inside of the array and is a parent cat
<?php } else { ?>
//category is not inside the array and It's not a parent either
<?php } ?>
Комментарии:
1. Так в чем проблема?
2. @Anant Я всегда
//category is inside of the array and is a parent cat
не понимаю, в какой категории я нахожусь
Ответ №1:
Похоже, ты забыл global
<?php
global $best_cats;
$this_category = get_category($cat);
$cat_id = $this_category->cat_ID;
if (in_array($cat_id, $best_cats) amp;amp; ($this_category->category_parent == 0 )) {
//category is inside of the array and is a parent cat
} else {
//category is not inside the array and It's not a parent either
}