php Wordpress - 特色图片元框未显示在自定义帖子类型上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13071202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:44:21  来源:igfitidea点击:

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

phpwordpressthumbnailscustom-post-type

提问by Ashley Staggs

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 的列。

enter image description here

在此处输入图片说明

Anyone know what is going on?

有谁知道发生了什么?

Thanks

谢谢

回答by Simon

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')以确定设置是否真的按预期设置,这将指向仅与管理部分相关的问题。

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 #postimagedivis in the markup but not displaying.

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

UPDATE:

更新

I just pasted the following code in at the end of functions.phpof 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.

我刚刚在functions.php211 主题的末尾粘贴了以下代码,在没有激活插件的 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');

回答by Xuwel Khan

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

我有同样的问题。我使用“自定义帖子类型 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 得到了这个代码!!

回答by user2366765

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.

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

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

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

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.

希望有帮助。

回答by gorelog

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.

可能想尝试禁用所有插件并重新打开它们,看看是否有一个插件有问题。

回答by crowjonah

wordpress screen options

wordpress 屏幕选项

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

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

回答by Patrick Jones

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_supportwould be nice. Anyway, I hope this helps someone and saves them from a frustrating evening.

在下面的代码片段中,我首先确定主题支持是什么,然后将我的自定义类型添加到列表中。通过在您的插件中执行此操作,它将与其他友好的主题或插件兼容。事实上,我认为 asafe_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');
}

回答by Ashley Staggs

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.

谢谢你们的帮助。