2015-09-18 2 views
0

Im получает сообщение об ошибке по следующему адресу: http://www.greenmonkeypublicrelations.com/citypads/apartments/the-ivinghoe-2-bedroom-1-bathroom-apartment/Недопустимый аргумент Ошибка

Цели здесь заключается в определении, если пост имеет ребенок, если у него есть дети, чтобы перечислить условия этих детей и эхо постоянной ссылки на пост в качестве href и эхо название термина в качестве текста для показа. Также, если сообщение является дочерним, затем получить родительский пост и перечислить его, чтобы показать ссылки на обоих уровнях. Heres мой код:

<?php 
     if(has_children()){ 
      $postid = get_the_ID(); 
      $args = array(
       'post_parent' => $postid, 
       'post_type' => 'any', 
       'numberposts' => -1, 
       'post_status' => 'any' 
      ); 
      $children_array = get_children($args, OBJECT); 
      foreach ($children_array as $child) { 
       $terms = get_the_terms($child->ID, 'Apartment_type'); 
       foreach ($terms as $term) { 
        echo '<a href="' . $child->guid . '">' . $term->name . '</a><br/>'; 
       } 
      }  
     } elseif (!has_children()) { 
      if (wp_get_post_parent_id(get_the_id())){ 
       $postid = wp_get_post_parent_id(get_the_id());  
       $args = array(
        'post_parent' => $postid, 
        'post_type' => 'any', 
        'numberposts' => -1, 
        'post_status' => 'any' 
       ); 
       $children_array = get_children($args, OBJECT); 
       foreach ($children_array as $child) { 
        $terms = get_the_terms($child->ID, 'Apartment_type'); 
        foreach ($terms as $term) { 
         echo '<a href="' . $child->guid . '">' . $term->name . '</a><br/>'; 
        } 
       } 
      }     
     } elseif((!has_children()) && (!get_post_ancestors($post->ID))) { 
      echo 'on its own'; 
     } ?> 
+0

что возвращаемое значение get_children ($ арг, OBJECT); если у него нет детей? пустой массив или false или ...? – swidmann

+0

Вы хотите увидеть var_dump ($ children_array)? –

+0

да, и get_the_terms ($ child-> ID, 'Apartment_type'); Я думаю, что один из них не является массивом – swidmann

ответ

0

Проблема заключается в том, что вы ожидаете $children_array или $terms всегда arrays. foreach ожидает массив, поэтому, если значение $children_array или $terms имеет значение (т. е.) false, вы получите эту ошибку.

Вы можете попробовать это:

<?php 
if(has_children()){ 
    $postid = get_the_ID(); 
    $args = array(
     'post_parent' => $postid, 
     'post_type' => 'any', 
     'numberposts' => -1, 
     'post_status' => 'any' 
    ); 
    $children_array = get_children($args, OBJECT); 
    if(is_array($children_array)) { 
     foreach ($children_array as $child) { 
      $terms = get_the_terms($child->ID, 'Apartment_type'); 
      if(is_array($terms)) { 
       foreach ($terms as $term) { 
        echo '<a href="' . $child->guid . '">' . $term->name . '</a><br/>'; 
       } 
      } 
     } 
    } 
} elseif (!has_children()) { 
    if (wp_get_post_parent_id(get_the_id())){ 
     $postid = wp_get_post_parent_id(get_the_id()); 
     $args = array(
      'post_parent' => $postid, 
      'post_type' => 'any', 
      'numberposts' => -1, 
      'post_status' => 'any' 
     ); 
     $children_array = get_children($args, OBJECT); 
     if(is_array($children_array)) { 
      foreach ($children_array as $child) { 
       $terms = get_the_terms($child->ID, 'Apartment_type'); 
       if(is_array($terms)) { 
        foreach ($terms as $term) { 
         echo '<a href="' . $child->guid . '">' . $term->name . '</a><br/>'; 
        } 
       } 
      } 
     } 
    } 
} elseif((!has_children()) && (!get_post_ancestors($post->ID))) { 
    echo 'on its own'; 
} ?> 
+0

Это, похоже, решило его, получило новую ошибку относительно чего-то еще, но не связанного. В чем проблема? –

+0

Проблема в том, что вы ожидаете, что child_array или термины всегда являются массивами. Foreach ожидает массив, поэтому, если child_array или terms имеют значение (т. Е.) False, вы получите эту ошибку – swidmann

Смежные вопросы