In the following code, $i
is not going to be your category ID.
foreach($slidercats_array AS $i => $value)
{
$cat = get_category($i); // cat_ID
if ( empty( $cat ) )
unset($slidercats_array[$i]);
}
At least I don’t see how it could be. If you explode
a string the keys in the resulting array are numerical and sequentially generated starting at 0. I assume that your original string is a comma separated list of category ids. That would mean that the key
s of the array– the $i
s– are
going to have no relationship to your category ids. For example:
$arr="9,8,7,6,5,4"; // assume these to be category IDs
print_r(explode(',',$arr));
/* Returns
Array
(
[0] => 9
[1] => 8
[2] => 7
[3] => 6
[4] => 5
[5] => 4
) */
You want to be using the $value
not the $i
. The $i
is the [0],[1],[2]
, etc. You want the second half– the $value
— when you check for the category (or term).
$cat = get_category($value); // cat_ID
You still want the $i
when you unset
though.
You seem to be making the same mistake in the second block as well and I am fairly sure that is incorrect use of the array keys is the root of the problem.