Site logo

Archived topic

Custom Post Type Featured Image

9 replies · Started by Dominik on December 2, 2020

Viewing posts 1–10 of 10

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 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 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 Tom,

THANKS! Works :D

Best wishes,
Dominik

You're welcome :)

This archived topic is closed to new replies.