无效的参数错误

时间:2015-09-18 10:07:20

标签: php wordpress foreach

我在以下网址收到错误消息: http://www.greenmonkeypublicrelations.com/citypads/apartments/the-ivinghoe-2-bedroom-1-bathroom-apartment/

这里的目的是确定帖子是否有孩子,如果有孩子,列出这些孩子的条款,并将帖子的永久链接作为href回显,并将术语标题作为要显示的文本回显。此外,如果帖子是孩子,那么获取父帖并列出相同的内容以显示两个级别上的链接。 继承我的代码:

<?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';  
        } ?>

1 个答案:

答案 0 :(得分: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';
} ?>