Created
November 17, 2016 08:30
-
-
Save smartdeal/b9ce5632507ba9863c5d8aff9eeb6ea8 to your computer and use it in GitHub Desktop.
wordpress Apply template to custom-post-type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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'] ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment