【问题标题】:Wordpress set featured image with custom post typeWordpress 使用自定义帖子类型设置特色图片
【发布时间】:2016-01-10 15:27:06
【问题描述】:

在 wp-admin 中,我创建了一个新的管理菜单页面。

add_menu_page('My custom post page type title', 'My custom post type menu', '', 'my-custom-slug', '', '', 99);
add_submenu_page('my-custom-slug', 'Add new', 'Add new', 'manage_options', 'post-new.php?post_type=my-custom-post-type', '');

我想使用像 post-new.php 和 edit.php 这样的面板,所以我注册了一个自定义帖子类型。

register_post_type('my-custom-post-type', 
                   array('labels'=>array('name'=>__('Products','text-domain'),
                                         'singular_name'=>__('Product','text-domain'),
                                         'menu_name'=>_x('Products','Admin menu name','text-domain'),
                                         'add_new'=>__('Add Product','text-domain'),
                                         'add_new_item'=>__('Add New Product','text-domain'),
                                         'edit_item'=>__('Edit Product','text-domain'),
                                         'new_item'=>__('New Product','text-domain'),
                                         'view_item'=>__('View Product','text-domain'),
                                         'not_found'=>__('No Products found','text-domain'),
                                         'not_found_in_trash'=>__('No Products found in trash','text-domain')),
                         'supports'=>array('title','editor','thumbnail','comments')
                         'rewrite'=>array('slug'=>'mscases'),
                         'public'=>true,
                         'capability_type'=>'post'));

自定义菜单页面可以正常工作,元框Featured Image也可以正常工作,我可以在媒体库中选择图片。

我选择一张图片后,它没有出现在 Featured Image 元框上,admin-ajax.php 响应为-1(我查看帖子页面,如果成功则为零)。

但是如果我把参数my-custom-post-type改成product(比如woocommerce),就会出现我捡到的特色图片。

我在编码中有什么遗漏的吗?

【问题讨论】:

    标签: php ajax wordpress


    【解决方案1】:

    更换试试

    register_post_type('my-custom-post-type',
        array('labels' => array('name' => __('Products', 'text-domain'),
                'singular_name' => __('Product', 'text-domain'),
                'menu_name' => _x('Products', 'Admin menu name', 'text-domain'),
                'add_new' => __('Add Product', 'text-domain'),
                'add_new_item' => __('Add New Product', 'text-domain'),
                'edit_item' => __('Edit Product', 'text-domain'),
                'new_item' => __('New Product', 'text-domain'),
                'view_item' => __('View Product', 'text-domain'),
                'not_found' => __('No Products found', 'text-domain'),
                'not_found_in_trash' => __('No Products found in trash', 'text-domain')),
            'supports' => array('title', 'editor', 'thumbnail', 'comments'),
            'rewrite' => array('slug' => 'mscases'),
            'public' => true,
            'capability_type' => 'post'));
    

    【讨论】:

      【解决方案2】:

      当您创建自定义帖子类型时,切勿使用“-”符号,请使用“_”而不是“-”。

      我过去有同样的问题,我已经改变并且它对我有用。

      【讨论】:

        【解决方案3】:

        google了一下这个问题,终于找到问题所在了。

        您必须使用add_action 并将挂钩设置为init

        add_action('init', array('MyClass', 'RegisterPostType'));

        特色图片效果很好。

        【讨论】:

          【解决方案4】:

          你必须先告诉WordPress你的主题支持特色图片,方法是把它放在functions.php中:

          add_action( 'after_setup_theme', 'umbrella_theme_setup' );
          function umbrella_theme_setup(){
              add_theme_support('post-thumbnails');
          }
          

          而且您需要在自定义帖子上启用特色图片:

          register_post_type('my-custom-post-type', 
                             array('labels'=>array('name'=>__('Products','text-domain'),
                                                   'singular_name'=>__('Product','text-domain'),
                                                   'menu_name'=>_x('Products','Admin menu name','text-domain'),
                                                   'add_new'=>__('Add Product','text-domain'),
                                                   'add_new_item'=>__('Add New Product','text-domain'),
                                                   'edit_item'=>__('Edit Product','text-domain'),
                                                   'new_item'=>__('New Product','text-domain'),
                                                   'view_item'=>__('View Product','text-domain'),
                                                   'not_found'=>__('No Products found','text-domain'),
                                                   'not_found_in_trash'=>__('No Products found in trash','text-domain')),
                                   'supports'=>array('title','editor','thumbnail','comments')
                                   'rewrite'=>array('slug'=>'mscases'),
                                   'public'=>true,
                                   'capability_type'=>'post'));
          

          特色图片元现在应该出现在您的自定义帖子中,the_post_thumnail(); 函数应该可以工作。 而且您必须删除 sub_menu_page 和 menu_page,因为 WordPress 会为它们显示 UI,这样您就不必准备了。

          【讨论】:

            猜你喜欢
            • 2013-10-27
            • 2021-12-26
            • 1970-01-01
            • 2014-04-20
            • 1970-01-01
            • 2015-04-03
            • 1970-01-01
            • 2015-02-10
            • 1970-01-01
            相关资源
            最近更新 更多