简体   繁体   English

PHP显示Wordpress数组。

[英]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. 如果您对Wordpress一无所知,但知道如何显示存储在php数组中的所有内容(至少在我的情况下)-请回答。 I'll appreciate! 我会感激的!

I've an PHP array that keeps lists of categories. 我有一个保留类别列表的PHP数组。 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 回声$ category [0]-> cat_name

  2. echo $category[1]->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". cat_name是“ cat_name”,“ Folio”(我的自定义帖子类型名称),“ type”,“ types”和“ my_folio_cat”。 Everything outputs nothing (even not "Array" text). 一切都不输出(即使不是“ Array”文本)。

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). 您还可以查看var_dump() (不打算阅读)和var_export() (甚至更少)。

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

try var_dump($category); 尝试var_dump($category); instead echo $category; 而是echo $category;

array access is echo $arrayname[0]; 数组访问是echo $ arrayname [0];

make var_dump($array) and you can see what is there. 制作var_dump($ array),您可以看到其中的内容。

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. 对于自定义分类法,应使用get_the_terms函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM