Support

Account

Home Forums ACF PRO How to show field True/False in admin column for custom post type

Solved

How to show field True/False in admin column for custom post type

  • I have created two custom columns for the admin screen of a custom post type I created.

    The first column displays an image from the an ACF field and it’s working fine. I have a second column named “Featured”. I’m trying to show either “Yes” or “No” depending if the field is checked. The field is using a True/False checkbox. If the user checks this box “Featured” I want the column to display “Yes” and it the field is not check it should display “No”.

    Here’s my custom columns code:

    add_filter( 'manage_edit-model_columns', 'my_columns_filter', 10, 1 );
    function my_columns_filter( $columns ) {
     	$column_thumbnail = array( 'thumbnail' => 'Model Photo' );
    	$column_featured = array( 'Featured' => 'Featured Model' );
    	
    	$columns = array_slice( $columns, 0, 1, true ) + $column_thumbnail + array_slice( $columns, 1, NULL, true );
    	$columns = array_slice( $columns, 0, 3, true ) + $column_featured + array_slice( $columns, 3, NULL, true );
    	
    	return $columns;
    }
    add_action( 'manage_posts_custom_column', 'my_column_action', 10, 1 );
    function my_column_action( $column ) {
    	global $post;
    	
    $model_pic = get_field('model_photo');
    
    	switch ( $column ) {
    		case 'thumbnail':
    			if ($model_pic) {
    			echo '<img src="' . $model_pic . '" width="125" />';
    			break;
    			} else {
        echo '<div style="width:123px;height:186px;background:rgba(0,0,0,.05);line-height:188px;text-align:center;border:1px solid rgba(0,0,0,.04);"><span style="padding:0px 15px;word-wrap: normal !important;">No Photo</span></div>';
    }
    	}
    }

    I tried adding this to the code:

    elseif($column == 'featured')
    	{
    		if(get_field('featured'))
    		{
    			echo 'Yes';
    		}
    		else
    		{
    			echo 'No';
    		}
    	}

    So the full code is:

    add_filter( 'manage_edit-model_columns', 'my_columns_filter', 10, 1 );
    function my_columns_filter( $columns ) {
     	$column_thumbnail = array( 'thumbnail' => 'Model Photo' );
    	$column_featured = array( 'Featured' => 'Featured' );
    	
    	$columns = array_slice( $columns, 0, 1, true ) + $column_thumbnail + array_slice( $columns, 1, NULL, true );
    	$columns = array_slice( $columns, 0, 3, true ) + $column_featured + array_slice( $columns, 3, NULL, true );
    	
    	return $columns;
    }
    add_action( 'manage_posts_custom_column', 'my_column_action', 10, 1 );
    function my_column_action( $column ) {
    	global $post;
    	
    $model_pic = get_field('model_photo');
    
    	switch ( $column ) {
    		case 'thumbnail':
    			if ($model_pic) {
    			echo '<img src="' . $model_pic . '" width="125" />';
    			break;
    			} else {
        echo '<div style="width:123px;height:186px;background:rgba(0,0,0,.05);line-height:188px;text-align:center;border:1px solid rgba(0,0,0,.04);"><span style="padding:0px 15px;word-wrap: normal !important;">No Photo</span></div>';
    }
    
    	}
    }
    if($column == 'featured')
    	{
    		if(get_field('featured'))
    		{
    			echo 'Yes';
    		}
    		else
    		{
    			echo 'No';
    		}
    	}

    The column is showing up but there’s nothing displayed.

    Blank Column

  • maybe just a stupid question:
    would it work if the column name of featured is lowercase?
    $column_featured = array( 'featured' => 'Featured' );

    and at frontend:
    did this code works?

    if(get_field('featured')) {
    echo 'Yes';
    } else {
    echo 'No';
    }
  • Wow, sorry for that. It was the uppercase “F” and yes the code:

    if(get_field('featured')) {
    echo 'Yes';
    } else {
    echo 'No';
    }

    does work. Thanks Mediawerk

  • I generally prefer using custom code rather than plugins when trying to accomplish these sorts of things, but I wonder if you’ve heard of Admin Columns Pro? It is insanely cool for doing these sorts of things and has direct ACF integration. I can’t even tell you how much time it has saved me. I have no affiliation with them at all, just passing on some useful info!

  • Thank you Dalton,

    I have checked it out and it does look very interesting. I maybe purchasing it if the need comes up for any further custom column issues.

    The export to php feature makes it very nice for developer’s in fact that’s why I purchased ACF Pro (the export to php feature).

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How to show field True/False in admin column for custom post type’ is closed to new replies.