Template Page Attributes

Page Template Attributes for Custom Post Type [WP Support]

wprocket

Hey there, I am here for today for a quick tutorial. Recently, we have switched the Genesis Framework to the Focusblog theme by Thrivetheme.

That’s why we had to update many designs and functions. Template page attributes for custom post type is one of them.

We have two custom post types registered, “Code Snippet” and “Case Studies.” I wanted to use the existing theme template for these post types. But, Template from Page Attributes available for only ‘page’ post type.

So, here are what we did to achieve that.

Enable TEMPLATE Page Attributes for CPT on WordPress

To do this, you’ll need to open the code you wrote to register custom post type(CPT). In my case, the following is the example code I added.

/*
 * Register Post Type 'Project'
 *
 */
function project_post_type() {
    
    // Labels
    $labels = array(
        'name'	=> _x( 'Projects', 'post type general name' ),
        'singular_name' => _x( 'Project', 'post type singular name' ),
        'menu_name' => 'Case Studies',
        'add_new' => _x( 'Add Project', 'project item' ),
        'add_new_item' => __( 'Add New Project' ),
        'edit_item' => __( 'Edit Project' ),
        'new_item' => __( 'New Project' ),
        'view_item' => __( 'View Project' ),
        'search_items' => __( 'Search Projects' ),
        'not_found' =>  __( 'No Projects Found' ),
        'not_found_in_trash' => __( 'No Projects Found in Trash' ),
        'parent_item_colon' => ''
    );
    
    // Register post type
    register_post_type( 'projects' , array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-awards',
        'rewrite' => array( 'slug' => 'project' ),
        'menu_position' => 5,
        'hierarchical' => true,
        'capability_type'    => 'page',
        'supports' => array(
                        'title',
                        'editor',
                        'excerpt',
                        'genesis-seo',
                        'custom-fields',
                        'page-attributes'
                    )
    ) );
}

add_action( 'init', 'project_post_type', 0 );

If you look at the code, you’ll notice “‘hierarchical’ => true,“. It is one of the arguments which is false by default. We need it to make true when we added “‘page-attributes’” to supports parameter.

Add “‘hierarchical’ => true,” or edit if “‘hierarchical’ => false,” to “‘hierarchical’ => true,“. Add “‘page-attributes’” to the supports parameter. As sooner as you save the file after adding this, you’ll see Page Attributes in the sidebar, like the screenshot below.

Page Attributes for CPT

Add Template Post Type to Existing Templates

Alright, now we are in the next/final part of the job. One of the fantastic enhancements was delivered in WordPress 4.7 for the template system. Using that enhancement, we can handle any template for any custom post type(CPT).

wprocket-728x98_Black

So, here is the part we are going to play around. The theme which we are using for RainaStudio has BLANK, FULL-WIDTH, LANDING, & NARROW page template. So, I open all templates one by one and add “Template Post Type: post, page, projects, code” after “Template Name.”

Here is the original code snippet, before adding template post type.

<?php
/*
  Template Name: Full Width
 */
?>
<?php
$options = thrive_get_theme_options();

$main_content_class = ( $options['sidebar_alignement'] == "right" ) ? "main_content" : "right_main_content";

And the snippet after adding template post type.

<?php
/*
  Template Name: Full Width
  Template Post Type: post, page, projects, code
 */
?>
<?php
$options = thrive_get_theme_options();

$main_content_class = ( $options['sidebar_alignement'] == "right" ) ? "main_content" : "right_main_content";

As sooner as I saved the file, code had taken into action. And the image below of Template selection.

Template Page Attributes

Conclusion

Being a thinker without box, I am not good at writing a conclusion. So far, if the tutorial works for you, share it with your friends and WordPress community. And your comment always much appreciated.

You will love The following tutorials:

GenesisPro728x90

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest
Share on tumblr
Tumblr
Anwer Ashif

Anwer Ashif

Founder of RainaStudio. Help businesses and individuals to create and outstand their online presence. Our success rate is measurable. Our blog served all around the world and counting.

2 thoughts on “Page Template Attributes for Custom Post Type [WP Support]

  1. Please help me i have cpt set up in place and even it shows in editor blocks but my problem is i want to link my custom post type to wordpress original posts which is not happening.

    Example: i Have default wordpress post in place(Post title: example 123) and i have custom post type called ramsethu in place(Post title: example abc). Now i want example 123 as parent for example abc.

    Currentlt i am only getting option to link within the same post types.

    Please help me !!!!

Leave a Reply

Your email address will not be published.

Want to speed up WordPress site?