Archived topic
Custom Post Type Featured Image
9 replies · Started by Dominik on December 2, 2020
Hi there,
we have created several custom post types and are wondering how we get the featured image and the text excerpt for those posts?
We added the custom post types using the childs functions.php with register_post_type.
Thanks in advance,
Dominik
Hi there,
The featured image option should inherit the settings in the customizer:
https://docs.generatepress.com/article/adjusting-the-featured-images/
Or do you want the setting to be different in the customizer and the CPT?
As for the excerpt, try this snippet:
https://generatepress.com/forums/topic/cpt-in-search-results-with-no-read-button/#post-967510
But replace false with true
Hi Leo,
the settings should be the same as stated in the customizer. But when I add a new post in my CPT, I do not have the option for adding a featured image.
This is my code:
/* --------------------------------------------------------- Custom Post Types */
function create_posttype() {
register_post_type( 'erlebnisse',
array(
'labels' => array(
'name' => __( 'Erlebnisse' ),
'singular_name' => __( 'Erlebnis' )
),
'taxonomies' => array( 'art' ),
'public' => true,
'has_archive' => false,
'menu_position' => 5,
'rewrite' => array('slug' => 'natur-erleben', "with_front" => false),
'show_in_rest' => true,
'menu_icon' => 'dashicons-palmtree',
)
);
register_post_type( 'angebote',
array(
'labels' => array(
'name' => __( 'Angebote' ),
'singular_name' => __( 'Angebot' )
),
'taxonomies' => array( 'art' ),
'public' => true,
'has_archive' => false,
'menu_position' => 4,
'rewrite' => array('slug' => 'areal-und-angebote', "with_front" => false),
'show_in_rest' => true,
'menu_icon' => 'dashicons-carrot',
)
);
}
add_action( 'init', 'create_posttype');
function createCustomTaxonomy() {
$bereicheLabels = array(
'name' => _x( 'Erlebnisse', 'taxonomy general name' ),
'singular_name' => _x( 'Art', 'taxonomy singular name' ),
'search_items' => __( 'Art Suchen' ),
'all_items' => __( 'Alle Arten' ),
'parent_item' => __( 'Eltern Art' ),
'parent_item_colon' => __( 'Eltern Art:' ),
'edit_item' => __( 'Art Bearbeiten' ),
'update_item' => __( 'Update Art' ),
'add_new_item' => __( 'Art hinzufügen' ),
'new_item_name' => __( 'Neue Art Name' ),
'menu_name' => __( 'Arten' ),
);
register_taxonomy('art',array('erlebnis'), array(
'hierarchical' => true,
'labels' => $bereicheLabels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'erlebnisart' ),
));
$artenLabels = array(
'name' => _x( 'Angebote', 'taxonomy general name' ),
'singular_name' => _x( 'Art', 'taxonomy singular name' ),
'search_items' => __( 'Art Suchen' ),
'all_items' => __( 'Alle Arten' ),
'parent_item' => __( 'Eltern Art' ),
'parent_item_colon' => __( 'Eltern Art:' ),
'edit_item' => __( 'Art Bearbeiten' ),
'update_item' => __( 'Update Art' ),
'add_new_item' => __( 'Art hinzufügen' ),
'new_item_name' => __( 'Neue Art Name' ),
'menu_name' => __( 'Arten' ),
);
register_taxonomy('art',array('angebot'), array(
'hierarchical' => true,
'labels' => $artenLabels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'angebotsart' ),
));
}
add_action( 'init', 'createCustomTaxonomy', 0 );
add_filter( 'generate_show_excerpt', function( $show ) {
if ( is_post_type_archive( 'Erlebnisse' ) || ( is_search() && 'Erlebnisse' === get_post_type() ) ) {
return true;
}
return $show;
} );
Hi there,
see here for the register_post_type arguments:
https://developer.wordpress.org/reference/functions/register_post_type/
You could add this argument to include the thumbnail:
'supports' => array('thumbnail')
Hi David,
as soon as I add the line, the gutenberg editor disappears. I have made a Screenshot
My goal is to get all the settings (with featured image, block editor etc.) in my custom post types identical to the standard posts of generatepress.
Thanks for your efforts
Dominik
Odd, can you share your entire code with that argument in place? Maybe its missing a comma after it ?
Sure :)
function create_posttype() {
register_post_type( 'erlebnisse',
array(
'labels' => array(
'name' => __( 'Erlebnisse' ),
'singular_name' => __( 'Erlebnis' )
),
'taxonomies' => array( 'art' ),
'public' => true,
'has_archive' => false,
'menu_position' => 5,
'rewrite' => array('slug' => 'natur-erleben', "with_front" => false),
'show_in_rest' => true,
'menu_icon' => 'dashicons-palmtree',
'supports' => array('thumbnail'),
)
);
register_post_type( 'angebote',
array(
'labels' => array(
'name' => __( 'Angebote' ),
'singular_name' => __( 'Angebot' )
),
'taxonomies' => array( 'art' ),
'public' => true,
'has_archive' => false,
'menu_position' => 4,
'rewrite' => array('slug' => 'areal-und-angebote', "with_front" => false),
'show_in_rest' => true,
'menu_icon' => 'dashicons-carrot',
'supports' => array('thumbnail'),
)
);
}
add_action( 'init', 'create_posttype');
function createCustomTaxonomy() {
$bereicheLabels = array(
'name' => _x( 'Erlebnisse', 'taxonomy general name' ),
'singular_name' => _x( 'Art', 'taxonomy singular name' ),
'search_items' => __( 'Art Suchen' ),
'all_items' => __( 'Alle Arten' ),
'parent_item' => __( 'Eltern Art' ),
'parent_item_colon' => __( 'Eltern Art:' ),
'edit_item' => __( 'Art Bearbeiten' ),
'update_item' => __( 'Update Art' ),
'add_new_item' => __( 'Art hinzufügen' ),
'new_item_name' => __( 'Neue Art Name' ),
'menu_name' => __( 'Arten' ),
);
register_taxonomy('art',array('erlebnis'), array(
'hierarchical' => true,
'labels' => $bereicheLabels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'erlebnisart' ),
));
$artenLabels = array(
'name' => _x( 'Angebote', 'taxonomy general name' ),
'singular_name' => _x( 'Art', 'taxonomy singular name' ),
'search_items' => __( 'Art Suchen' ),
'all_items' => __( 'Alle Arten' ),
'parent_item' => __( 'Eltern Art' ),
'parent_item_colon' => __( 'Eltern Art:' ),
'edit_item' => __( 'Art Bearbeiten' ),
'update_item' => __( 'Update Art' ),
'add_new_item' => __( 'Art hinzufügen' ),
'new_item_name' => __( 'Neue Art Name' ),
'menu_name' => __( 'Arten' ),
);
register_taxonomy('art',array('angebot'), array(
'hierarchical' => true,
'labels' => $artenLabels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'angebotsart' ),
));
}
add_action( 'init', 'createCustomTaxonomy', 0 );
add_filter( 'generate_show_excerpt', function( $show ) {
if ( is_post_type_archive( 'Erlebnisse' ) || ( is_search() && 'Erlebnisse' === get_post_type() ) ) {
return true;
}
return $show;
} );
Hi there,
You need to add support for the editor as well:
'supports' => array( 'editor', 'thumbnail' ),
You can read more about the supports arg here: https://developer.wordpress.org/reference/functions/register_post_type/
Hope it helps!
Hi Tom,
THANKS! Works :D
Best wishes,
Dominik
You're welcome :)