WordPress Thumbnails in Custom Post Type

1

I am using the following code to enable Custom Posts Types on my site. No functions. What should I add to this code so that the function of thumbnails to the custom post appears?

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'portfolio',
array(
  'labels' => array(
    'name' => __( 'Portfolio' ),
    'singular_name' => __( 'Porfolio' )
  ),
  'public' => true,
)
  );
}
register_taxonomy(
"categorias", 
  "portfolio", 
  array(            
    "label" => "Categorias", 
        "singular_label" => "Categoria", 
        "rewrite" => true,
        "hierarchical" => true
)
);
    
asked by anonymous 05.07.2016 / 21:33

1 answer

1

Just in the second parameter of register_post_type() , you add the following:

'supports' => array( 'thumbnail ')

As you can see in the documentation, the array supports is where you put everything your custom post will support. Note that for this to work, your theme must have support for thumbnails enabled. This can be done in the functions.php file by placing

add_theme_support( 'post-thumbnails' );

I also noticed that your source code does not have the action needed to register the custom taxonomy ( as documentation shows). So, assuming you have not yet registered it, and for everything to work (and fixing some indentation errors), your code should look like this

add_action( 'init', 'create_post_type' );
add_action( 'init', 'register_custom_taxonomy ');

function create_post_type() {

    register_post_type( 'portfolio', 
        array(
            'labels' => array(
                'name' => __( 'Portfolio' ),
                'singular_name' => __( 'Porfolio' )
            ),
            'public' => true,
            'supports' => array( 'thumbnail ')
        )
    );
}

function register_custom_taxonomy(){

    register_taxonomy( 'categorias', 'portfolio', 
        array(
            'label' => 'Categorias', 
            'singular_label' => 'Categoria', 
            'rewrite' => true,
            'hierarchical' => true
        )
    );
}
    
06.07.2016 / 02:44