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.

Read More

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:

  1. echo $category[0]->cat_name

  2. 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));

Related posts

Leave a Reply

5 comments

  1. 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 />';
    }
    
  2. 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;
    }