Custom Fields
Re-Envisioned
Support MB Admin Columns Displaying Featured Image in Admin Columns

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 3 weeks ago.

  • Creator
    Topic
  • #16225
    Resolved
    John Anderson
    Participant

    Hi guys,
    I have followed along with some custom code I found in this forum trying to display the featured_image in the admin columns before the Title. But it breaks my site.

    I don’t have a theme, so I don’t use functions.php. Instead I use Code Snippets to add custom PHP.

    Is there some code somewhere that provides a working example of how to display the featured image in the Admin Columns?

    Thanks for your time.

    John Anderson.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #16241

    Anh Tran
    Keymaster

    Hi John,

    Please try this snippet:

    add_action( 'admin_init', function() {
        class My_Featured_Image_Columns extends MB_Admin_Columns_Post {
            public function columns( $columns ) {
                $columns  = parent::columns( $columns );
                $position = 'before';
                $target   = 'title';
                $this->add( $columns, 'featured_image', 'Featured Image', $position, $target );
                // Add more if you want
                return $columns;
            }
            public function show( $column, $post_id ) {
                switch ( $column ) {
                    case 'featured_image':
                        the_post_thumbnail( [40, 40] );
                        break;
                    // More columns
                }
            }
        }
    
        new My_Featured_Image_Columns( 'post', array() );
    } );
    #16250

    John Anderson
    Participant

    Oh! I love you!!! lol.

    And you updated the documentation for Admin Columns with this example! Now I just change the ‘post’ slug to what ever slug my CPT is and it will display the featured image in the admin columns.

    Thank you!

    Just one last question.

    new My_Featured_Image_Columns( 'post', array() );
    when I instantiate the class, how would I do so for more than 1 CPT? I tried this but it didn’t work.

    new My_Featured_Image_Columns( array('team-member','challenge'), array() );   
    

    So I am guessing that 2 instantiations are necessary.

    new My_Featured_Image_Columns( 'team-member', array() );
    new My_Featured_Image_Columns( 'challenge', array() );

    Thank you,
    John.

    #16257

    Anh Tran
    Keymaster

    Hi John,

    You’re right about 2 instantiations. Because the class is designed for single post type, we can’t pass an array to its constructor. Initializing it twice for different post types does the job.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.