WordPress® - 特色图片Meta Box未在自定义帖子类型上显示

时间:2021-01-14 16:38:28

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.

奇怪的是,在列出我的帖子类型的条目的页面上,有一个名为Thumbnail的列。

WordPress® - 特色图片Meta Box未在自定义帖子类型上显示

Anyone know what is going on?

有谁知道发生了什么事?

Thanks

7 个解决方案

#1


19  

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

默认情况下禁用缩略图,WordPress Codex在此处明确说明,

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.

确保您还在主题/插件中的某处完成了add_theme_support('post-thumbnails'),或者您的帖子类型位于提供给上述函数的帖子类型列表中(第二个参数是一个可选的帖子类型数组)您已按照帖子类型启用它。

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.

似乎精选帖子的“屏幕选项”设置可以设置为隐藏/显示每种帖子类型。虽然它很遥远,它可能已被停用,但我猜它应默认激活。还要尝试检查post_type_supports('project','thumbnail')的返回值,以确定该设置是否实际按预期设置,这将指向该问题仅与admin部分相关。

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.

也许你可以在你的主题/插件中运行if语句,并确保它按预期返回true。如果是,您可能还需要检查编辑页面的源,以查看#postimagediv是否在标记中但不显示。

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.

我刚刚在Twenty Eleven主题的functions.php结尾处粘贴了以下代码,在没有插件激活的WordPress 3.4.2安装上,它工作正常 - 类型出现了,我能够看到帖子缩略图编辑屏幕中的元框。

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');

#2


3  

i've got same problem. i used "custom post type ui" plugin for creating a 'portfolio' post type. i tried lot of things, but didn't work. Finally i've tried this code

我有同样的问题。我使用“custom post type ui”插件来创建“投资组合”帖子类型。我尝试了很多东西,但没有用。最后我试过这段代码

add_action('init', 'my_custom_init');
    function my_custom_init() {
        // 'portfolio' is my post type, you replace it with yours
        add_post_type_support( 'portfolio', 'thumbnail' ); 
    }

it worked !! i've got this code from codex!!

有效 !!我从codex获得了这段代码!

#3


2  

If you're running a custom theme, that theme may have a theme_support call somewhere in its custom files that might be overriding your theme support call.

如果您正在运行自定义主题,则该主题可能会在其自定义文件中的某处调用theme_support,这可能会覆盖您的主题支持调用。

If You can track down that track down that theme's call, you can copy it to your own theme file and then add your custom post type to it.

如果您可以跟踪该主题调用的该跟踪,则可以将其复制到您自己的主题文件中,然后将自定义帖子类型添加到其中。

You can put it inside a function and then use an action hook, like after_setup_theme.

你可以将它放在一个函数中,然后使用一个动作钩子,比如after_setup_theme。

here's an example of a custom theme original support call:

这是自定义主题原始支持调用的示例:

add_theme_support('post-thumbnails', array('slide-items','post','gallery-items','audio-items','video-items','page','event-items',

I was running a child theme off that main theme and needed a custom post type called 'staff'. Even though i declared support for that custom post type to include thumbnails, the featured image meta box wasn't showing.

我在主题上运行了一个子主题,需要一个名为“staff”的自定义帖子类型。即使我声明支持该自定义帖子类型以包含缩略图,但特色图像元框未显示。

I added the following code to my child theme functions.php file. Notice, I added 'staff' at the end of the function.

我将以下代码添加到我的子主题functions.php文件中。请注意,我在函数末尾添加了“staff”。

add_action( 'after_setup_theme', 'add_theme_support' );

function add_theme_support (){
    add_theme_support('post-thumbnails', array('slide-items','post','gallery-items','audio-items','video-items','page','event-items','staff'));
    }

Hope that helps.

希望有所帮助。

#4


0  

WordPress® - 特色图片Meta Box未在自定义帖子类型上显示

Make sure you have Featured Image set to Show on Screen in the Screen Options on the Post Editor page

确保在“帖子编辑器”页面的“屏幕选项”中将“特色图像”设置为“在屏幕上显示”

#5


0  

I've run into this problem a couple of times. I disabled the BackupBuddy plugin and the Featured Image meta box came back. May not work in your instance, but hopefully this helps someone else.

我曾经遇到过这个问题几次。我禁用了BackupBuddy插件,并返回了精选图像元框。可能无法在您的实例中工作,但希望这有助于其他人。

May want to try disabling all your plugins and turning them back on to see if one by one to see if it is a problem with a plugin.

可能想尝试禁用所有插件并重新打开它们以查看是否一个接一个地查看插件是否存在问题。

#6


0  

I realize this is an older question, but none of these solutions worked for me. It turned out there were two problems, first: multiple plugins trying to call add_theme_support. The second was that they assumed certain types, or needed knowledge of the theme when adding support.

我意识到这是一个较老的问题,但这些解决方案都不适合我。原来有两个问题,第一个:多个插件试图调用add_theme_support。第二个是他们在添加支持时假设某些类型或需要主题知识。

In the following code snippet I am safely first determining what the theme support is, and then adding my custom type to the list. By doing this in your plugin it will be compatible with other friendly themes or plugins. In fact I think a safe_add_theme_support would be nice. Anyway, I hope this helps someone and saves them from a frustrating evening.

在下面的代码片段中,我首先确定主题支持是什么,然后将我的自定义类型添加到列表中。通过在您的插件中执行此操作,它将与其他友好的主题或插件兼容。事实上,我认为safe_add_theme_support会很好。无论如何,我希望这可以帮助某人并使他们远离令人沮丧的夜晚。

$currentPostThumbnails = get_theme_support('post-thumbnails');
if(is_array($currentPostThumbnails)) {
    add_theme_support( 'post-thumbnails', array_merge($currentPostThumbnails, array( 'mytype' )) );
}else{
    add_theme_support( 'post-thumbnails', 'mytype');
}

#7


-1  

Well I seem to have solved the problem. I was running 3.4.2, so I deleted all the wordpress installation files (except wp-config.php and my themes), and then used the upgrade feature to get to 3.4.2 again. On 3.4.1 it works, but on 3.4.2 it doesn't.

好吧,我似乎已经解决了这个问题。我运行3.4.2,所以我删除了所有的wordpress安装文件(除了wp-config.php和我的主题),然后使用升级功能再次进入3.4.2。在3.4.1它可以工作,但在3.4.2它没有。

I have downgraded again, and will wait for a future update. All I can say is this is one weird bug.

我已经再次降级,并将等待未来的更新。我只能说这是一个奇怪的错误。

Thanks for you help guys.

谢谢你的帮助。

#1


19  

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

默认情况下禁用缩略图,WordPress Codex在此处明确说明,

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.

确保您还在主题/插件中的某处完成了add_theme_support('post-thumbnails'),或者您的帖子类型位于提供给上述函数的帖子类型列表中(第二个参数是一个可选的帖子类型数组)您已按照帖子类型启用它。

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.

似乎精选帖子的“屏幕选项”设置可以设置为隐藏/显示每种帖子类型。虽然它很遥远,它可能已被停用,但我猜它应默认激活。还要尝试检查post_type_supports('project','thumbnail')的返回值,以确定该设置是否实际按预期设置,这将指向该问题仅与admin部分相关。

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.

也许你可以在你的主题/插件中运行if语句,并确保它按预期返回true。如果是,您可能还需要检查编辑页面的源,以查看#postimagediv是否在标记中但不显示。

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.

我刚刚在Twenty Eleven主题的functions.php结尾处粘贴了以下代码,在没有插件激活的WordPress 3.4.2安装上,它工作正常 - 类型出现了,我能够看到帖子缩略图编辑屏幕中的元框。

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');

#2


3  

i've got same problem. i used "custom post type ui" plugin for creating a 'portfolio' post type. i tried lot of things, but didn't work. Finally i've tried this code

我有同样的问题。我使用“custom post type ui”插件来创建“投资组合”帖子类型。我尝试了很多东西,但没有用。最后我试过这段代码

add_action('init', 'my_custom_init');
    function my_custom_init() {
        // 'portfolio' is my post type, you replace it with yours
        add_post_type_support( 'portfolio', 'thumbnail' ); 
    }

it worked !! i've got this code from codex!!

有效 !!我从codex获得了这段代码!

#3


2  

If you're running a custom theme, that theme may have a theme_support call somewhere in its custom files that might be overriding your theme support call.

如果您正在运行自定义主题,则该主题可能会在其自定义文件中的某处调用theme_support,这可能会覆盖您的主题支持调用。

If You can track down that track down that theme's call, you can copy it to your own theme file and then add your custom post type to it.

如果您可以跟踪该主题调用的该跟踪,则可以将其复制到您自己的主题文件中,然后将自定义帖子类型添加到其中。

You can put it inside a function and then use an action hook, like after_setup_theme.

你可以将它放在一个函数中,然后使用一个动作钩子,比如after_setup_theme。

here's an example of a custom theme original support call:

这是自定义主题原始支持调用的示例:

add_theme_support('post-thumbnails', array('slide-items','post','gallery-items','audio-items','video-items','page','event-items',

I was running a child theme off that main theme and needed a custom post type called 'staff'. Even though i declared support for that custom post type to include thumbnails, the featured image meta box wasn't showing.

我在主题上运行了一个子主题,需要一个名为“staff”的自定义帖子类型。即使我声明支持该自定义帖子类型以包含缩略图,但特色图像元框未显示。

I added the following code to my child theme functions.php file. Notice, I added 'staff' at the end of the function.

我将以下代码添加到我的子主题functions.php文件中。请注意,我在函数末尾添加了“staff”。

add_action( 'after_setup_theme', 'add_theme_support' );

function add_theme_support (){
    add_theme_support('post-thumbnails', array('slide-items','post','gallery-items','audio-items','video-items','page','event-items','staff'));
    }

Hope that helps.

希望有所帮助。

#4


0  

WordPress® - 特色图片Meta Box未在自定义帖子类型上显示

Make sure you have Featured Image set to Show on Screen in the Screen Options on the Post Editor page

确保在“帖子编辑器”页面的“屏幕选项”中将“特色图像”设置为“在屏幕上显示”

#5


0  

I've run into this problem a couple of times. I disabled the BackupBuddy plugin and the Featured Image meta box came back. May not work in your instance, but hopefully this helps someone else.

我曾经遇到过这个问题几次。我禁用了BackupBuddy插件,并返回了精选图像元框。可能无法在您的实例中工作,但希望这有助于其他人。

May want to try disabling all your plugins and turning them back on to see if one by one to see if it is a problem with a plugin.

可能想尝试禁用所有插件并重新打开它们以查看是否一个接一个地查看插件是否存在问题。

#6


0  

I realize this is an older question, but none of these solutions worked for me. It turned out there were two problems, first: multiple plugins trying to call add_theme_support. The second was that they assumed certain types, or needed knowledge of the theme when adding support.

我意识到这是一个较老的问题,但这些解决方案都不适合我。原来有两个问题,第一个:多个插件试图调用add_theme_support。第二个是他们在添加支持时假设某些类型或需要主题知识。

In the following code snippet I am safely first determining what the theme support is, and then adding my custom type to the list. By doing this in your plugin it will be compatible with other friendly themes or plugins. In fact I think a safe_add_theme_support would be nice. Anyway, I hope this helps someone and saves them from a frustrating evening.

在下面的代码片段中,我首先确定主题支持是什么,然后将我的自定义类型添加到列表中。通过在您的插件中执行此操作,它将与其他友好的主题或插件兼容。事实上,我认为safe_add_theme_support会很好。无论如何,我希望这可以帮助某人并使他们远离令人沮丧的夜晚。

$currentPostThumbnails = get_theme_support('post-thumbnails');
if(is_array($currentPostThumbnails)) {
    add_theme_support( 'post-thumbnails', array_merge($currentPostThumbnails, array( 'mytype' )) );
}else{
    add_theme_support( 'post-thumbnails', 'mytype');
}

#7


-1  

Well I seem to have solved the problem. I was running 3.4.2, so I deleted all the wordpress installation files (except wp-config.php and my themes), and then used the upgrade feature to get to 3.4.2 again. On 3.4.1 it works, but on 3.4.2 it doesn't.

好吧,我似乎已经解决了这个问题。我运行3.4.2,所以我删除了所有的wordpress安装文件(除了wp-config.php和我的主题),然后使用升级功能再次进入3.4.2。在3.4.1它可以工作,但在3.4.2它没有。

I have downgraded again, and will wait for a future update. All I can say is this is one weird bug.

我已经再次降级,并将等待未来的更新。我只能说这是一个奇怪的错误。

Thanks for you help guys.

谢谢你的帮助。