1

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:

  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));
2
  • from what I recall of get_the_category, $category[0]->cat_name should do it. Try var_dump($category) to see the structure of the variable, and it would help to edit your post and add that info here. It's not just arrays here, just to note - some of this info is stored in a PHP object. Commented Dec 6, 2010 at 16:57
  • You should use name instead of cat_name (which is deprecated, and only currently implemented for back-compat) Commented Dec 6, 2010 at 18:36

5 Answers 5

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 />';
}
Sign up to request clarification or add additional context in comments.

Comments

0

var_dump I do believe.

http://php.net/manual/en/function.var-dump.php

3 Comments

Note that var_dump() prints variables, whereas print_r() is intended to be a human readable representation of the variables
Oh I've only ever used var_dump but then again I may not be human.
@Rafe Kettler I don't find var_dump to be unreadable. It only provides more detail than print_r.
0

try var_dump($category); instead echo $category;

Comments

0

array access is echo $arrayname[0];

make var_dump($array) and you can see what is there.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.