将自定义列添加到多个自定义帖子类型

时间:2021-02-03 20:50:04

标签: php wordpress

我添加了 3 种自定义帖子类型,对于每种帖子类型,我都有 ACF 字段,我希望在每种帖子类型的列中显示该字段。我已经添加了代码,但如果在 foreach 循环中添加,则奶头不起作用。

这是代码。

$cpt_list = array(
    'book','movie','presentation',
);

foreach ( $cpt_list as $key ) {
    add_filter( "manage_edit-'.$key.'_posts_columns", function($columns) {
        unset(
            $columns['date']
        );
        $columns['custom_pass'] = __( 'Custom Password', 'theme' );
        $columns['date']        = esc_html__( 'Date', 'theme' );

        return $columns;

        }
    );
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我在过滤器钩子上犯了一个小错误。

$cpt_list = array(
    'book','movie','presentation',
);

foreach ( $cpt_list as $key ) {
    add_filter( 'manage_'.$key.'_posts_columns', function($columns) {
        unset(
            $columns['date']
        );
        $columns['custom_pass'] = __( 'Custom Password', 'theme' );
        $columns['date']        = esc_html__( 'Date', 'theme' );

        return $columns;

        }
    );
}

成功了。