启用自定义帖子类型的帖子缩略图

时间:2016-03-17 作者:clestcruz

我正在编写一个自定义帖子类型作为插件,并试图找出如何在插件文档/模板中启用帖子缩略图,而不是在函数中启用它。php。我尝试在自定义贴子插件中添加下面的代码。php和它似乎不起作用。我仍然需要在函数中编写代码。php为我的自定义帖子启用帖子缩略图

/ * ADD THUMBNAIL FOR POST
 * ------------------------------------------------------------------------ */
add_theme_support( \'post-thumbnails\', array( \'post\', \'custom-post\' ) );
下面的示例代码

<?php
/*
Plugin Name: Example Custom Post Plugin
Description: Example post plugin for News and Press custom post
*/
?>

<?php
/* ------------------------------------------------------------------------ *
 * EXAMPLE CUSTOM POST
  ------------------------------------------------------------------------ */

function example () {

// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Example\', \'Post Type General Name\', \'twentythirteen\' ),
        \'singular_name\'       => _x( \'Example\', \'Post Type Singular Name\', \'twentythirteen\' ),
        \'menu_name\'           => __( \'Example\', \'twentythirteen\' ),
        \'parent_item_colon\'   => __( \'Parent Example\', \'twentythirteen\' ),
        \'all_items\'           => __( \'All Post\', \'twentythirteen\' ),
        \'view_item\'           => __( \'View Post\', \'twentythirteen\' ),
        \'add_new_item\'        => __( \'Add New Post\', \'twentythirteen\' ),
        \'add_new\'             => __( \'Add Post\', \'twentythirteen\' ),
        \'edit_item\'           => __( \'Edit Post\', \'twentythirteen\' ),
        \'update_item\'         => __( \'Update Post\', \'twentythirteen\' ),
        \'search_items\'        => __( \'Search Post\', \'twentythirteen\' ),
        \'not_found\'           => __( \'Not Found\', \'twentythirteen\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'twentythirteen\' ),
    );

// Set other options for Custom Post Type

    $args = array(
        \'label\'               => __( \'Example\', \'twentythirteen\' ),
        \'description\'         => __( \'Example\', \'twentythirteen\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        \'taxonomies\'          => array(\'category\', \'post_tag\'),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 4,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );

    // Registering your Custom Post Type
    register_post_type( \'example\', $args );
}

/* Hook into the \'init\' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/

add_action( \'init\', \'example\', \'example\', 1 );

add_action( \'after_setup_theme\', \'example_featured_image\');
function example_featured_image() {
        add_theme_support( \'post-thumbnails\', array( \'post\', \'page\', \'production_firm\', \'example\' ) );
}

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

您很可能正在使用add_theme_support() 在错误的上下文中(挂钩)。

因为你是registering the post type 您自己需要做的就是在supports 将参数传递给时的数组register_post_type()

supports

(数组/布尔值)(可选)用于调用的别名add_post_type_support() 直接地从3.5开始,布尔false可以作为值而不是数组传递,以防止出现默认(标题和编辑器)行为。

默认值:标题和编辑器

你只需要通过thumbnail 到阵列

\'supports\' => [\'title\', \'editor\', \'thumbnail\'], // Add additional values as needed
您还需要check if support for thumbnails is enabled 根据主题,如果没有,请在插件中添加帖子缩略图支持。

相关推荐

GET_POSTS查询大约需要40秒来执行

我在get\\u帖子中有一个元查询,它需要花很长时间才能完成。它工作得很好,但只是时间太长了。我有一个名为event. 在每个event 发布后,有自定义元数据:post\\U sort\\U日期(事件日期YmdHis 格式,用于排序)我需要做的是获取下一个事件,该事件相对于$year 和$month 变量。所以如果$year = 2021 和$month = 10 (2021 10月)然后应该在2021 11月或之后找到第一个事件。我下面的查询很好,但很慢。执行大约需要40秒,我不知道为什么。$next