2017-08-25 88 views
0

我在自定义帖子类型中有一个可重复字段,该类型链接到另一个自定义帖子类型。我想遍历可重复的字段,然后为每个字段访问链接的帖子类型中的数据。第一个结果返回,但在第二个我得到以下错误:尝试输出数组时出现PHP错误

Fatal error : [] operator not supported for strings.

我想从我的变量,比如$员工= $教练[“team_staff”]去掉括号,但没有奏效。

我也尝试设置$ staff = array();之前的循环,并没有奏效。

不知道我有什么错在这里:

global $post; 

// Get The Staff Members 
$coaches = get_post_meta($post->ID, 'repeatable_fields', true); 
if ($coaches) : 
    foreach ($coaches as $coach) : 
     $staff[] = $coach['team_staff']; 
     $role[] = $coach['team_role']; 

     // Loop through each staff member 
     foreach($staff as $index => $staff) : 
      $args = array (
       'post_type' => 'staff', 
       'title' => $staff 
      ); 

      $posts = get_posts($args); 
      foreach ($posts as $post) : setup_postdata ($post); 

       // get post meta here 

      endforeach; 
     endforeach; 

    endforeach; 
endif; 
+0

可能的重复:https://stackoverflow.com/questions/5879675/problem-with-fatal-error-operator-not-supported-for-strings-in – RToyo

+0

可能的重复[问题与:致命错误:\ [\]运算符不支持字符串](https://stackoverflow.com/questions/5879675/problem-with-fatal-error-operator-not-supported-for-strings-in) – aynber

+0

我注释了$角色[ ] = $ coach ['team_role'];并仍然出现错误。我尝试了上述文章的答案,并从$ staff = $ coach ['team_staff']中删除了[];并获得一个错误。 – RiotAct

回答

1

你需要注意你的循环变量名。例如改变$人员$ staff_member:

global $post; 

// Get The Staff Members 
$coaches = get_post_meta($post->ID, 'repeatable_fields', true); 
if ($coaches) : 
    foreach ($coaches as $coach) : 
     $staff[] = $coach['team_staff']; 
     $role[] = $coach['team_role']; 

     // Loop through each staff member 
     foreach($staff as $index => $staff_member) : 
      $args = array (
       'post_type' => 'staff', 
       'title' => $staff_member 
      ); 

      $posts = get_posts($args); 
      foreach ($posts as $post) : setup_postdata ($post); 

       // get post meta here 

      endforeach; 
     endforeach; 

    endforeach; 
endif; 

而且最好你应该初始化您的数组,$人员和$作用环外:

$staff = []; 
$role = []; 

而且,目前还不清楚,你为什么会一再增加$ staff数组并在每次迭代$ coaches数组时循环它。考虑将两个foreach循环和运行它们一个接一个:

global $post; 

// Get The Staff Members 
$coaches = get_post_meta($post->ID, 'repeatable_fields', true); 
if ($coaches) : 
    $staff = []; 
    $role = []; 

    foreach ($coaches as $coach) : 
     $staff[] = $coach['team_staff']; 
     $role[] = $coach['team_role']; 
    endforeach; 

    // Loop through each staff member 
    foreach($staff as $index => $staff_member) : 
     $args = array (
      'post_type' => 'staff', 
      'title' => $staff_member 
     ); 

     $posts = get_posts($args); 
     foreach ($posts as $post) : setup_postdata ($post); 

      // get post meta here 

     endforeach; 
    endforeach; 

endif; 
+0

谢谢。我想我需要我的foreach循环嵌套才能访问前一个循环的数据。现在我明白了。 – RiotAct

+0

当然。数组/变量不限于循环结构,而仅限于函数或全局。 –

0

您的代码设计是错误的:

  • 你的第二个的foreach嵌套在通过阵列$staff这是第一个和迭代填写第一个foreach。我不知道这是否是故意的,这意味着在第一个foreach的第一个循环中,第二个foreach将遍历一个条目,即之前放在两行中的条目,通过第二个循环中的两个条目等等...
  • 你的第二个foreach迭代通过$staff ,同时命名它当前使用的值为$staff。一旦第二个foreach被执行,你的数组以前被称为$staff已经不存在了,被第一个条目的值覆盖,这恰好是string

当第一foreach是做了第二循环,你的代码,因此试图使用索引运算符 - [] - 一个字符串,如错误说。

试试这个:

global $post; 

// Get The Staff Members 
$coaches = get_post_meta($post->ID, 'repeatable_fields', true); 
if ($coaches) : 
    $staffs = $roles = array(); 
    foreach ($coaches as $coach) : 
     $staffs[] = $coach['team_staff']; 
     $roles[] = $coach['team_role']; 
    } 
    // Loop through each staff member 
    foreach($staffs as $index => $staff) : 
     $args = array (
      'post_type' => 'staff', 
      'title' => $staff 
     ); 

     $posts = get_posts($args); 
     foreach ($posts as $post) : setup_postdata ($post); 
      // get post meta here 
     } 
    } 
} 

我加了复数的数组,这样它更明显,它们包含几个角色/员工,因为你应用了相同的规则命名您的其他阵列$coaches并且其命名值$coachforeach

+0

你只是比其他答案迟了几分钟,但这也有帮助。谢谢! – RiotAct

+0

@RiotAct重点是你摆脱了麻烦。 – ksjohn

+1

还有一个简单的问题:我后来作为$ role [$ index]访问$角色,现在我将如何访问它? Nevermind:我明白了:$ roles [$ index]; – RiotAct

相关问题