Display custom post types with multiple page templates

I’ve created a custom post type like :

    register_post_type( 'foobar' ,
        'labels' => array(
            'name' => __('Foobar'),
            'singular_name' => __('Foobar')
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-welcome-write-blog',
        'supports' => ['title', 'editor', 'thumbnail']

After that, I’ve created multiple page templates :




* Template Name: whatever
* Template Post Type: post, page, product, foobar

I’d usually output the custom post type with something like:

<?php $args = array( 'post_type' => 'foobar', 'posts_per_page' => 1, 'orderby' => 'meta_value' );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                echo '<p>';
                echo '</p>';
            endwhile; ?>

I’m creating two new posts in my custom post type foobar and as a template for one post foo and the other post bar.

How can I output multiple page templates, created in one CPT on the same page?
Like, let’s say, I’ve got 7 posts in the CPT, which all have different templates and they have to be outputted on the same page.

Could it actually work like that?

Read more here: Display custom post types with multiple page templates

Leave a Reply

Your email address will not be published. Required fields are marked *