[Resolved] Block Element Content Template for custom post type

Home Forums Support [Resolved] Block Element Content Template for custom post type

Home Forums Support Block Element Content Template for custom post type

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1807162
    Manuel

    Hi there,

    as we have been using custom post types (with custom slugs) quit often in the last months, I was wondering if it is also possible to have a block element content template (like this: https://docs.generatepress.com/article/block-element-content-template/) also for the custom post type overview.

    At the moment we are using WP show posts Pro – but it would be nice to use the new functionalities with the block elements.

    Thanks and best wishes,
    Dominik

    #1807224
    David
    Staff
    Customer Support

    Hi there,

    if you use the GP theme templates as the basis of your CPT Templates then yes you can use the Content Template in Elements.
    For example here in GP’s archive.php:

    https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/archive.php#L40

    The generate_do_template_part within then loop is what Block Element Content Template uses to switch out the template.

    #1807276
    Manuel

    Hi David,

    thanks for the quick response!
    What do you mean with “use GP theme templates”? I create a custom post type like

    /* Referenzen Custom Post Type */
    if ( ! function_exists('custom_post_type') ) {
    
    // Register Custom Post Type
    function custom_post_type() {
    
    	$labels = array(
    		'name'                  => _x( 'Referenzen', 'Post Type General Name', 'text_domain' ),
    		'singular_name'         => _x( 'Referenz', 'Post Type Singular Name', 'text_domain' ),
    		'menu_name'             => __( 'Referenzen', 'text_domain' ),
    		'name_admin_bar'        => __( 'Referenzen', 'text_domain' ),
    		'archives'              => __( 'Referenzen Archive', 'text_domain' ),
    		'attributes'            => __( 'Referenzen Attribute', 'text_domain' ),
    		'parent_item_colon'     => __( 'Eltern Referenzen', 'text_domain' ),
    		'all_items'             => __( 'Alle Referenzen', 'text_domain' ),
    		'add_new_item'          => __( 'Neue Referenz', 'text_domain' ),
    		'add_new'               => __( 'Neue Referenz', 'text_domain' ),
    		'new_item'              => __( 'Neue Referenz', 'text_domain' ),
    		'edit_item'             => __( 'Referenz bearbeiten', 'text_domain' ),
    		'update_item'           => __( 'Referenz aktualisieren', 'text_domain' ),
    		'view_item'             => __( 'Zeige Referenz', 'text_domain' ),
    		'view_items'            => __( 'Zeige Referenzen', 'text_domain' ),
    		'search_items'          => __( 'Suche Referenz', 'text_domain' ),
    		'not_found'             => __( 'Nicht gefunden', 'text_domain' ),
    		'not_found_in_trash'    => __( 'Nicht im Papierkorb gefunden', 'text_domain' ),
    		'featured_image'        => __( 'Featured Image', 'text_domain' ),
    		'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
    		'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    		'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    		'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
    		'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    		'items_list'            => __( 'Items list', 'text_domain' ),
    		'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
    		'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    	);
    	$rewrite = array(
    		'slug'                  => 'referenzen',
    		'with_front'            => false,
    		'pages'                 => true,
    		'feeds'                 => true,
    	);
    	$args = array(
    		'label'                 => __( 'Referenz', 'text_domain' ),
    		'description'           => __( 'Referenzen', 'text_domain' ),
    		'labels'                => $labels,
            'show_in_rest' => true,
    		'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
    		'taxonomies'            => array( 'referenzen_taxonomy' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'menu_icon'             => 'dashicons-admin-home',
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => false,
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'query_var'             => 'post_type_referenz',
    		'rewrite'               => $rewrite,
    		'capability_type'       => 'page',
    	);
    	register_post_type( 'post_type_referenzen', $args );
    
    }
    add_action( 'init', 'custom_post_type', 0 );
    
    }

    and a content template…but where do I apply the template to?

    #1807598
    David
    Staff
    Customer Support

    So that code is registering the Custom Post Type.
    Generally you would a PHP Template for the displaying that post type – see here for example on setting up a single templates:

    https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/

    For the archives to work you would need to:

    1. Change this CPT arg 'has_archive' => false,to 'has_archive' => true,

    2. Set up an archive PHP with your slug:

    https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/#custom-post-type-templates

    You can simply copy the themes archive.php and save it as your archive-your-slug.php

    Then you would be able to display an Archive page that could be rebuilt using the Block Element.

    #1809661
    Manuel

    Hi David,

    thanks for the great support, as always!
    I will give it a try 🙂

    #1809764
    David
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.