This is more of a basic PHP question, but still it’s somewhat important. Inside your foreach
loop, $category
is a locally-defined variable. That’s to say, it only exists inside the foreach
loop.
So if you have
foreach ( $categories as $category ) {
// You can use $category all you want in here
}
// Out here $category ceases to exist
This is just the way iterative loops work … Even if the variable still existed outside of the foreach
you have no way to know which $category
you’re referencing.
From the looks of things, though the $category
you want is your portfolio category (I’m guessing it has a category ID of 104 based on your earlier code).
You can still fetch this category based on its ID, just be sure to give it a unique name rather than $category
:
$parent_category = get_category( 104 );
$parent_category_name = $parent_category->name;
You can then chug merrily along.