Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Featured Image Meta Box not showing on custom post type

I just created a custom post type, but for some reason the Featured Image meta box isn't showing up.

It does show on the "posts" post type though.

I have enabled theme support for thumbnails and have added the following code in my custom post type code.

<?php

function register_cpt_product() {

    $labels = array( 
        'name' => _x( 'Products', 'product' ),
        'singular_name' => _x( 'Product', 'product' ),
        'add_new' => _x( 'Add New', 'product' ),
        'add_new_item' => _x( 'Add New Product', 'product' ),
        'edit_item' => _x( 'Edit Product', 'product' ),
        'new_item' => _x( 'New Product', 'product' ),
        'view_item' => _x( 'View Product', 'product' ),
        'search_items' => _x( 'Search Products', 'product' ),
        'not_found' => _x( 'No products found', 'product' ),
        'not_found_in_trash' => _x( 'No products found in Trash', 'product' ),
        'parent_item_colon' => _x( 'Parent Product:', 'product' ),
        'menu_name' => _x( 'Products', 'product' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Allows the user to create products',
        'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'product', $args );
}

add_action( 'init', 'register_cpt_product' );

?>

The weird thing is, that on the pages which lists my entries for my post type, there is a column called Thumbnail.

enter image description here

Anyone know what is going on?

Thanks

like image 508
Ashley Staggs Avatar asked Oct 25 '12 14:10

Ashley Staggs


1 Answers

Thumbnails are by default disabled, the WordPress Codex explicitly states so here,

Themes have to declare their support for post thumbnails before the interface for assigning these images will appear on the Edit Post and Edit Page screens.

Make sure you have also done add_theme_support('post-thumbnails') somewhere in your theme/plugin, or that your post type is in the list of post types supplied to the above function (second argument is an optional array of post types) if you are already enabling it per post type.

It appears the "Screen options" setting for Featured post can be set to hide/show per post type. Though it's far fetched it might have been deactivated, though it should be activated by default I guess. Also try checking the return value of post_type_supports('project', 'thumbnail') to determine if the setting is actually set as intended, which would point to the issue being related to the admin section only.

The featured post meta box is added to the admin section by the follow lines of code:

if ( current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) )
    add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', null, 'side', 'low');

Perhaps you could run that if-statement in your theme/plugin and make sure it returns true as intended. In case it is, you might also want to inspect the edit page's source to see if #postimagediv is in the markup but not displaying.

UPDATE:

I just pasted the following code in at the end of functions.php of the Twenty Eleven theme, on a WordPress 3.4.2 install with no plugins activated, and it worked fine - the type showed up and I was able to see the post thumbnail meta box in the edit screen.

add_theme_support('post-thumbnails');
function setup_types() {
    register_post_type('mytype', array(
        'label' => __('My type'),
        'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'show_ui' => true,
    ));
}
add_action('init', 'setup_types');
like image 85
Simon Avatar answered Sep 24 '22 12:09

Simon