Ich schreibe einen benutzerdefinierten Post-Typ als Plugin und versuche herauszufinden, wie ich Post-Thumbnails in der Plugin-Dokumentation/-Vorlage aktivieren kann, anstatt sie in der functions.php zu aktivieren. Ich habe versucht, den folgenden Code in custom-post-plugin.php einzufügen und es scheint nicht zu funktionieren. Ich muss den Code noch in die functions.php schreiben, um die Miniaturansichten für meinen benutzerdefinierten Beitrag zu aktivieren
/ * ADD THUMBNAIL FOR POST
* ------------------------------------------------------------------------ */
add_theme_support( 'post-thumbnails', array( 'post', 'custom-post' ) );
Beispielcode unten
<?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' ) );
}
Sie verwenden add_theme_support()
höchstwahrscheinlich im falschen Kontext (hook).
Da Sie den Beitragstyp registrieren sich selbst, müssen Sie lediglich die Unterstützung für Miniaturansichten im Array supports
hinzufügen, wenn Sie Ihre Argumente an register_post_type()
übergeben.
supports
(array/boolean) (optional) Ein Alias für den direkten Aufruf von
add_post_type_support()
. Ab 3.5 kann boolean false anstelle eines Arrays als Wert übergeben werden, um das Standardverhalten (Titel und Editor) zu verhindern.Standard: Titel und Editor
Sie müssen lediglich thumbnail
an das Array übergeben
'supports' => ['title', 'editor', 'thumbnail'], // Add additional values as needed
Sie müssten auch prüfen, ob die Unterstützung für Miniaturansichten aktiviert ist nach dem Thema und, falls nicht, Post-Thumbnail-Unterstützung in Ihrem Plugin hinzufügen.