wordpress多维数组的访问值

时间:2017-06-15 07:57:27

标签: php arrays wordpress multidimensional-array

对于我的一个项目,我创建了一个自定义的帖子类型和分类链。 我尝试在每个自定义帖子中访问所述分类法的值,使用此代码我得到一个包含所述值的多维数组:

<?php
            $specs = wp_get_post_terms($post->ID, 'specializzazioni', array("fields" => "all"));
            print_r($specs);
 ?>

输出例如:

         Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 6
                [name] => Chirurgia Toracica
                [slug] => chirurgia-toracica
                [term_group] => 0
                [term_taxonomy_id] => 6
                [taxonomy] => specializzazioni
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
            )

    [1] => WP_Term Object
        (
            [term_id] => 7
            [name] => Oculistica Pediatrica
            [slug] => oculistica-pediatrica
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => specializzazioni
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

我只是试图访问[name]和[slug]的值,但我似乎无法找到办法。 使用下面的循环输出每个值,但这不是我正在寻找的

 <?php 
foreach($specs as $row => $value){
    foreach($value as $row2 => $value2)
        echo $value2 . "<br/>";
}
?>

我尝试在每个变量上使用名称和slug键,但每次我得到不同类型的错误。

1 个答案:

答案 0 :(得分:1)

请注意,该数组包含对象。您应该能够为每个对象访问nameslug,如下所示:

foreach ($specs as $object) {
  echo $object->name . ' ' . $object->slug;
}