尝试输出数组时出现 PHP 错误 [英] Php error when trying to output array

查看:29
本文介绍了尝试输出数组时出现 PHP 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a repeatable field in a wordpress custom post type that is linked to another wordpress custom post type. I want to loop through the repeatable field and then for each field access the data from the linked post type. The first result is returned, but on the second I get the following error:

致命错误:字符串不支持 [] 运算符.

Fatal error : [] operator not supported for strings.

我尝试从我的变量中删除括号,例如 $staff = $coach['team_staff'] 但这不起作用.

I tried removing the brackets from my variables like $staff = $coach['team_staff'] but that did not work.

我也试过设置 $staff = array();在循环之前,这不起作用.

I also tried setting $staff = array(); before the loop and that did not work.

不确定我这里有什么问题:

Not sure what I have wrong here:

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;

推荐答案

你需要小心你的循环变量名.例如将 $staff 更改为 $staff_member:

You need to be careful with your loop variable names. For example change $staff to $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:

Also ideally you should initialise your arrays, $staff and $role outside the loops:

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

此外,不清楚为什么要重复添加到 $staff 数组并在 $coaches 数组的每次迭代中循环遍历它.考虑将两个 foreach 循环分开并一个接一个地运行它们:

Also, it's unclear why you would repeatedly add to the $staff array and loop over it with each iteration of the $coaches array. Consider separating the two foreach loops and run them one after the other:

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;

这篇关于尝试输出数组时出现 PHP 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆