Make custom post type display as a page

I am trying to make my custom post type display on the front end as if it were a page. Depending on the theme, it won’t have the date, author, etc…

I’ve looked into creating a template such as ‘single-<CPT>.php’. But that’s too static to work for any theme design.

Is it possible to somehow tell the current theme to display the custom post type in the same way it would display a normal page.

Thanks!

2 Answers
2

There are a number of template filters you can use to alter the template hierarchy. Have a look at the template hierarchy page at the filters and example provided.

Here’s a modified version of the example that uses single_template. It checks for a specific custom post type and loads the theme’s page.php template if it exists:

function wpa54721_single_template( $templates="" ){

    // get data for the current post and check type
    $this_post = get_queried_object();
    if( 'my_custom_post_type' == $this_post->post_type ):

        // $templates could be empty, a string, or an array,
        // we need to check and alter it appropriately

        if( !is_array( $templates ) && !empty( $templates ) ):
            // it's a string
            $templates = locate_template( array( 'page.php', $templates ), false );
        elseif( empty( $templates ) ):
            // it's empty
            $templates = locate_template( 'page.php', false );
        else:
            // it's an array
            $new_template = locate_template( array( 'page.php' ) );
            if( !empty( $new_template ) ) array_unshift( $templates, $new_template );
        endif;

    endif;

    return $templates;
}
add_filter( 'single_template', 'wpa54721_single_template' );

Leave a Comment