If you know nothing about WordPress but know how to display everything stored in an php array (at least in my case) – please answer. I’ll appreciate!
I’ve an PHP array that keeps lists of categories. But I have no idea how to display its contents.
This code:
$category = get_the_category();
echo $category;
Outputs:
Array
What I want to do is to display first item in the array.
I’ve also tried:
-
echo $category[0]->cat_name
-
echo $category[1]->cat_name
Where the cat_name was “cat_name”, “Folio” (my custom post type name), “type”, “types” and “my_folio_cat”. Everything outputs nothing (even not “Array” text).
I’m registering taxonomy like that:
register_taxonomy("my_folio_cat", array("folio"), array("hierarchical" => true, "label" => "Type", "singular_label" => "Type", "rewrite" => true));
You can also take a look at
var_dump()
(not intended for reading) andvar_export()
(even less so).If you’d like to print things nicely, you could iterate over the array:
var_dump
I do believe.http://php.net/manual/en/function.var-dump.php
try
var_dump($category);
insteadecho $category;
array access is echo $arrayname[0];
make var_dump($array) and you can see what is there.
Based of your explanation, I guess you use custom taxonomy instead of general post category. For custom taxonomy you should use get_the_terms function.
So perhaps the code should: