I created a function that returns an array of Custom Post type taxonomies:
function arfolio_get_cpt_tax(){
$categories = get_terms( 'service_categories' );
$cat_array = array();
foreach( $categories as $cat ){
$cat_array[$cat->term_id] = $cat->name;
}
return $cat_array;
}
I call this function in page.php. It’s working fine. But When I am calling this function another file which is included in functions.php It’s getting me error:
Notice: Trying to get property of non-object in wordpresswp-contentthemesarfolio-wpinccpt.php on line 127
Please help me to fix this error.
The line 127 is in the above function:
$cat_array[$cat->term_id] = $cat->name;
Thanks
Check that
$categories
is an array of object, to show it, put the following code:It should shows you an objects array, if not, check what is the answer of the function, maybe is an array of arrays, and you should use something like:
get_terms
returnWP_Error
object if it did not success please doand check the error
it may be something like this
if so please check that your taxonomy is exist
Edit
it’s a case of your term fetching occurring before the taxonomy has been registered.
please check that you call your function after the that the taxonomy is registered
for example check that you include your file which calling the function is included after the registration of the taxonomy
I assume that you registered your taxonomy in the init action and the init action is called actually after the functions.php is included so any call of your function inside functions.php will not work
so you need to use this function inside templates or in template parts