PHP displaying Wordpress array.
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 outpu开发者_开发百科ts 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));
print_r($array);
You can also take a look at var_dump()
(not intended for reading) and var_export()
(even less so).
If you'd like to print things nicely, you could iterate over the array:
foreach($array as $key => $value) {
echo 'Key is '.$key.' for value '.$value.'<br />';
}
var_dump
I do believe.
http://php.net/manual/en/function.var-dump.php
try var_dump($category);
instead echo $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:
$cats = get_the_terms($post, 'my_folio_cat');
// display the first category name
if(!empty($cats)) {
echo $cats[0]->name;
}
精彩评论