douyuai8994 2017-06-15 07:57
浏览 41
已采纳

wordpress多维数组的访问值[重复]

For one of my projects I created a custom post type and taxonomy chained to it. I'm trying to access the values of said taxonomy in each of my custom posts, using this code I get a multidimensional array with said values:

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

output for example:

         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
        )

)

I'm only trying to access the values of [name] and [slug] but I can't seem to find a way to do that. Using the loop below outputs every value but that's not what I'm looking for

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

I tried using the name and slug keys on every variable but everytime I get a different type of error.

</div>
  • 写回答

1条回答 默认 最新

  • drq231358 2017-06-15 08:00
    关注

    Notice that the array contains objects. You should be able to access name and slug for each object like this:

    foreach ($specs as $object) {
      echo $object->name . ' ' . $object->slug;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题