Displaying Content From Custom Post Type with Page Templates

So, I’m developing a site for a client where the homepage is built like a one-page scroller, but I also need the functionality of additional pages outside of the single homepage. I’ve built a custom post type for these sections and used this code to display them on the homepage.

<?php query_posts( array('post_type'=>'homepage', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>

    <?php 
        global $post;
        $slug = $post->post_name;
        locate_template(
            array(
                "template-$slug.php",
                'template-main.php'
            ), true
        );
    ?>
<?php endwhile; endif; ?>

So, as you can see, this is automatically pulling content and displaying it using page templates based on the post slug, however, I need to allow my client to display the content based on a page template chosen in a dropdown and I’ve used this code to create a dropdown UI that displays the page templates.

Read More
add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
function add_custom_page_attributes_meta_box(){
global $post;
    if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
        add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
    }
}

function custom_page_attributes_meta_box($post) {
    $template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
    <select name="page_template" id="page_template">
        <?php $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' ); ?>
        <option value="default"><?php echo esc_html( $default_title ); ?></option>
        <?php page_template_dropdown($template); ?>
    </select><?php
}

add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
function save_custom_page_attributes_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
    if ( ! current_user_can( 'edit_post', $post_id ) ) return;
    if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
        update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
    }
}

So, the problem I’m facing now is how to display all the custom posts in my main page according to the chosen page template.

Thanks so much!
J

Related posts

1 comment

  1. WordPress actually only uses _wp_page_template meta field for page type posts. If you want to change the template, you can use the filter single template. One thing I would recommend is you place good notes that you are using this in your theme/ plugin….

    btw update cpt to your post-type

    function load_cpt_template($single_template) {
         global $post;
    
         if ($post->post_type == 'cpt') {
    
              $new_template = get_post_meta( $post->ID, '_wp_page_template', true ); 
    
              // if a blank field or not valid do nothing, load default..
              if( is_file($new_template) )
                $single_template = $new_template;
         }
         return $single_template;
    }
    add_filter( 'single_template', 'load_cpt_template' );
    

Comments are closed.