Wordpress Thumbnails in Custom Post Type

Asked

Viewed 282 times

1

I am using the following code to enable Custom Posts Types on my website. No functions. What should I add to this code for the thumbnails function to appear in the custom post?

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
)
);

1 answer

1


It is sufficient that in the second parameter of register_post_type(), you add the following:

'supports' => array( 'thumbnail ')

As can be seen in the documentation, the array supports is where you put all your custom post will support. Note that in order for this to work, your theme must be supported for thumbnails enabled. This can be done in the file functions.php, putting

add_theme_support( 'post-thumbnails' );

I also noticed that your source code does not have the action necessary to register the custom taxonomy (how the documentation shows). So, assuming Voce hasn’t registered it yet, 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
        )
    );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.