0

I've created a new post type gallery using CPT UI plugin, Now I want to add options to choose post Format and Featured Image in add new gallery post. Please check the attached screenshot. Could you please tell me how to get this option in add new post page.

enter image description here

4 Answers 4

1

First of all themes need to use add_theme_support() in the functions.php file to tell WordPress which post formats to support by passing an array of formats like so:

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

Note that you must call this before the init hook gets called! A good hook to use is the after_setup_theme hook.

Then by code you can add the post formats like below-

//add post-formats to post_type 'your_custom_post_type'
add_post_type_support( 'your_custom_post_type', 'post-formats' );

Also can add support on creation of post type like below-

// register custom post type 'your_custom_post_type' with 'supports' parameter
add_action( 'init', 'create_your_post_type' );
function create_my_post_type() {
    register_post_type( 'your_custom_post_type',
      array(
        'labels' => array( 'name' => __( 'Products' ) ),
        'public' => true,
        'supports' => array('title', 'editor', 'post-formats')
    )
  );
} 
6
  • Should I add this code in wp-admin/admin-functions.php ? Commented Nov 1, 2016 at 10:34
  • Nope. Add those to your theme's functions.php file.
    – CodeMascot
    Commented Nov 1, 2016 at 10:35
  • Thank you its working :) How about Featured Image option in add new gallery post ? Commented Nov 1, 2016 at 10:43
  • If this thing helped you will you please accept the answer. It would be very helpful. @SureshAlagar
    – CodeMascot
    Commented Nov 1, 2016 at 10:51
  • Please help. Check my answer below. I've created functions.php in my theme and added the code. But I am getting error message in my home page. Commented Nov 1, 2016 at 11:00
0

When you will add new Custom Post Type using CPT UI plugin, you will see an option under Settings -> Support -> Post Formats in CPT UI Plugin.

enter image description here

1
  • Yes, choosen. Still it is not getting displayed in add new gallery post Commented Nov 1, 2016 at 10:23
0

I've added the following code in funtions.php in my theme:

<?php

add_theme_support( 'post-formats', array( 'gallery', 'image', 'video' ) );

//add post-formats to post_type 'your_custom_post_type'
add_post_type_support( 'your_custom_post_type', 'post-formats' );

// register custom post type 'your_custom_post_type' with 'supports' parameter
add_action( 'init', 'create_your_post_type' );
function create_my_post_type() {
    register_post_type( 'your_custom_post_type',
      array(
        'labels' => array( 'name' => __( 'Products' ) ),
        'public' => true,
        'supports' => array('title', 'editor', 'post-formats')
    )
  );
}
0

After adding post format support to your CPT:

function jt_add_post_type_support() {

   //add post-formats to post_type 'your_custom_post_type'
   add_post_type_support( 'your_custom_post_type', 'post-formats' );
}
add_action( 'init', 'jt_add_post_type_support', 10 );

You can modify it when this: (*DO FORGET TO CHANGE "your_custom_post_type" to your CPT in both functions)

add_action( 'load-post.php',     'jt_post_format_support_filter' );
add_action( 'load-post-new.php', 'jt_post_format_support_filter' );
add_action( 'load-edit.php',     'jt_post_format_support_filter' );

function jt_get_allowed_project_formats() {
    //CHANGE TO THE ONES YOU WANT HERE:
    return array( 'standard', 'quote', 'image', 'video' );
}

function jt_default_post_format_filter( $format ) {

    return in_array( $format, jt_get_allowed_project_formats() ) ? $format : 'standard';
}

function jt_post_format_support_filter() {

    $screen = get_current_screen();

    // Bail if not on the projects screen.
    if ( empty( $screen->post_type ) ||  $screen->post_type !== 'your_custom_post_type' )
        return;

    // Check if the current theme supports formats.
    if ( current_theme_supports( 'post-formats' ) ) {

        $formats = get_theme_support( 'post-formats' );

        // If we have formats, add theme support for only the allowed formats.
        if ( isset( $formats[0] ) ) {
            $new_formats = array_intersect( $formats[0], jt_get_allowed_project_formats() );

            // Remove post formats support.
            remove_theme_support( 'post-formats' );

            // If the theme supports the allowed formats, add support for them.
            if ( $new_formats )
                add_theme_support( 'post-formats', $new_formats );
        }
    }

    // Filter the default post format.
    add_filter( 'option_default_post_format', 'jt_default_post_format_filter', 95 );
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.