Created
April 19, 2021 11:19
-
-
Save flyingwebie/bbbd37094ff8f79251716d8653b2a3fe to your computer and use it in GitHub Desktop.
Add Columns to Custom Post Type with ACF Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //remove opening php tag | |
// add column(s) to admin cpt ui | |
add_filter ( 'manage_cptname_posts_columns', 'wd_add_acf_columns' ); | |
function wd_add_acf_columns ( $columns ) { | |
return array_merge ( $columns, array ( | |
'additional_column_1' => __ ( 'Column 1 Label' ), | |
'additional_column_2' => __ ( 'Column 2 Label' ), | |
)); | |
} | |
// add data to column(s) | |
add_action ( 'manage_cptname_posts_custom_column', 'wd_cptname_custom_column', 10, 2 ); | |
function wd_cptname_custom_column ( $column, $post_id ) { | |
switch ( $column ) { | |
case 'additional_column_1': | |
$acf_value_1 = get_field( 'acf_value_1' ); | |
echo $acf_value_1; | |
break; | |
case 'additional_column_2': | |
$acf_value_2 = get_field( 'acf_value_2' ); | |
echo $acf_value_2; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment