0

I'm trying to create a custom post as a plugin and so far I'm having some trouble trying to enable featured image on my custom post type. Right now I have created two custom post types but the problem is that whenever I tried activating both of them the other custom post type featured image will be disabled. So far I cannot identify the root cause and also tried looking for answers on google

Here is the code I have been working on.

1st Custom Post Type (Slides)

<?php
/*
Plugin Name: Slides Custom Post Plugin
Description: Custom post plugin for Slides custom post
*/
?>
<?php
/* ------------------------------------------------------------------------ *
 * SLIDES CUSTOM POST
  ------------------------------------------------------------------------ */

function slides () {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Slides', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Slides', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Slides', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Slides', 'twentythirteen' ),
        'all_items'           => __( 'All Slides', 'twentythirteen' ),
        'view_item'           => __( 'View Slide', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Slide', 'twentythirteen' ),
        'add_new'             => __( 'Add Slide', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Slide', 'twentythirteen' ),
        'update_item'         => __( 'Update Slide', 'twentythirteen' ),
        'search_items'        => __( 'Search Slide', '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'               => __( 'Slide', 'twentythirteen' ),
        'description'         => __( 'Slide', '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( 'slides', $args );
}

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

add_action( 'init', 'slides', 1 );

/* Enable Featured Image */

add_action( 'after_setup_theme', 'slides_featured_image');
function slides_featured_image() {
        add_theme_support( 'post-thumbnails', array( 'post', 'page', 'slides' ) );
}
?>

2nd Custom Post Type (Articles)

<?php
/*
Plugin Name: News & Press Custom Plugin
Description: Custom post plugin for News and Press custom post
*/
?>
<?php
/* ------------------------------------------------------------------------ *
 * NEWS PRESS CUSTOM POST
  ------------------------------------------------------------------------ */

function news_press () {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'News & Press Release', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'News & Press Release', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'News & Press', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent News & Press Release', 'twentythirteen' ),
        'all_items'           => __( 'All Articles', 'twentythirteen' ),
        'view_item'           => __( 'View Article', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Article', 'twentythirteen' ),
        'add_new'             => __( 'Add Article', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Article', 'twentythirteen' ),
        'update_item'         => __( 'Update Article', 'twentythirteen' ),
        'search_items'        => __( 'Search Article', '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'               => __( 'News & Press Release', 'twentythirteen' ),
        'description'         => __( 'News & Press Release', '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( 'news_press', $args );
}

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

add_action( 'init', 'news_press', 1 );


/* Enable Featured Image */

add_action( 'after_setup_theme', 'news_press_featured_image');
function news_press_featured_image() {
        add_theme_support( 'post-thumbnails', array( 'post', 'page', 'news_press' ) );
}
?>
3
  • you use add_theme_support twice, the second instance overwrites what you set in the first instance.
    – Milo
    Mar 22, 2016 at 17:10
  • Is there a way to enable featured image for both plug-ins?
    – clestcruz
    Mar 22, 2016 at 23:37
  • you can add theme support without specifying the post types, the 2nd argument is optional. any post type that registers with thumbnail support will have featured images enabled.
    – Milo
    Mar 23, 2016 at 4:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.