• Hi all,

    So i got the template box voor Custom post type finally to work

    now the only thing is that it doesn’t really save to template i choose.
    If i choose the template it was save it, untill a update the page after editing it, then it goes back to the standard template. anybody got any ideas how to fix this. below my code:

    /********************* Custom Template Meta Box ***********************/
    
    function post_template_meta_box($post) {
    
        	if ( 'hiddenpage' == $post->post_type && 0 != count( get_post_templates() ) ) {
    		$template = !empty($post->post_template) ? $post->post_template : false;
        ?>
    <label class="screen-reader-text" for="post_template"><?php _e('Template') ?></label><select name="post_template" id="post_template">
    <option value='default'><?php _e('Default template'); ?></option>
    <?php post_template_dropdown($template); ?>
    </select>
    <?php
      } ?>
    <?php
    }
    add_action('add_meta_boxes','add_post_template_metabox');
    function add_post_template_metabox() {
        add_meta_box('postparentdiv', __('Template'), 'post_template_meta_box', 'hiddenpage', 'side', 'core');
    }
    function get_post_templates() {
      $themes = get_themes();
      $theme = get_current_theme();
      $templates = $themes[$theme]['Template Files'];
      $post_templates = array();
    
      if ( is_array( $templates ) ) {
        $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
    
        foreach ( $templates as $template ) {
          $basename = str_replace($base, '', $template);
          if ($basename != 'functions.php') {
            // don't allow template files in subdirectories
            if ( false !== strpos($basename, '/') )
              continue;
    
            $template_data = implode( '', file( $template ));
    
            $name = '';
            if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
              $name = _cleanup_header_comment($name[1]);
    
            if ( !empty( $name ) ) {
              $post_templates[trim( $name )] = $basename;
            }
          }
        }
      }
    
      return $post_templates;
    }
    function post_template_dropdown( $default = '' ) {
      $templates = get_post_templates();
      ksort( $templates );
      foreach (array_keys( $templates ) as $template )
        : if ( $default == $templates[$template] )
          $selected = " selected='selected'";
        else
          $selected = '';
      echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
      endforeach;
    }
    add_action('save_post','save_post_template',10,2);
    function save_post_template($post_id,$post) {
      if ($post->post_type=='hiddenpage' && !empty($_POST['post_template']))
        update_post_meta($post->ID,'_post_template',$_POST['post_template']);
    }
    add_filter('single_template','get_post_template_for_template_loader');
    function get_post_template_for_template_loader($template) {
      global $wp_query;
      $post = $wp_query->get_queried_object();
      if ($post) {
        $post_template = get_post_meta($post->ID,'_post_template',true);
        if (!empty($post_template) && $post_template!='default')
          $template = get_stylesheet_directory() . "/{$post_template}";
      }
      return $template;
    }

    thanks in advance

  • The topic ‘Template meta box for custom post type’ is closed to new replies.