我认为问题在于这一行:
$hero_image_id = isset( $post_parent ) && ( $assigned_hero_images = get_the_terms( $post_parent, \'hero_images\' ) ) && is_array( $assigned_hero_images ) && ( $hero_image = array_shift( $assigned_hero_images ) ) && isset( $hero_image->term_id ) ? $hero_image->term_id : NULL;
我想我明白你的意图,但PHP并没有按照你认为的顺序处理它。
让我试着分解一下:
isset( $post_parent ) // This isn\'t necessary since it couldn\'t not exist and still get into this code block.
$assigned_hero_images = get_the_terms( $post_parent, \'hero_images\' )
is_array( $assigned_hero_images ) // again, you just created it, so you don\'t need to check here.
$hero_image = array_shift( $assigned_hero_images ) // You\'re trying to access the contents of the first element of the array, but this isn\'t the best approach.
$hero_image->term_id
完成上述操作的清理版本如下所示:
$hero_image_id = get_the_terms( $post_parent, \'hero_images\')[0]->$term_id;
The
[0]
获取对第一个数组元素的访问权限
->$term_id
从对象获取该属性。您可以通过php中的一个名为
array dereferencing.
这是没有测试,所以我可能错过了一些东西,但希望它让你在球场上足够的解决它从这里。