2017-08-25 117 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

相關問題