Does anybody know how to add meta box template select to custom post type in WordPress?

does anybody know how I can achieve the following:

  • I have a custom post type called ‘projects’ I have 5 different page
    layouts (each with a different number of images at different sizes
    that are arranged differently). I think that if I can ID the images I
    can position them using CSS. If each layout has it’s own template page
    with containers for the required images and each also has CSS to
    position the images.
  • I am wondering if it’s possible to add a
    template select on right hand side of post editor view, like you have
    available for pages this way it’s possible to assign a template (from
    1-5) for each post.
  • I am adding custom meta boxes for the image upload fields
    and the other data in the main post editor, but ideally this should
    change for the other pages to only display the required upload
    fields.

Any ideas anybody? I did consider setting up a custom post type for each layout (the 5 templates) allowing you to post a new project under the layout type you require, then on the page query the loop with all 5 custom-post-types (layouts). I worry this might be messy though, because to find projects to edit later you will need to know which template page (custom-post-type) they belong to but more importantly the url to projects wouldn’t be www.sitename.co.uk/projects but instead be www.sitename.co.uk/custom-post-type1, www.sitename.co.uk/custom-post-type2 ??? Wouldn’t it?

Read More

Any help really appreciated as always. Thanks

Related posts

Leave a Reply

2 comments

  1. here is code I have identified so far, any ideas how to customise?

    Should this go in functions.php?

    // Check if the post has a special template
    $template = get_post_meta($post->ID, '_wp_mf_page_template', true);
    
    if (!$template || $template == 'default') {
    return;
    }
    
    $template = TEMPLATEPATH.'/'.$template;
    
    if ( $template = apply_filters( 'template_include', $template ) ) {
    include($template);
    die();
    }
    return;
    

    Second code:

    // Check if the post_type has page attributes
    // if is the case is necessary need save the page_template
    if ($_POST['post_type'] != 'page' && isset($_POST['page_template'])) {
      add_post_meta($post_id, '_wp_mf_page_template', $_POST['page_template'], true) or update_post_meta($post_id, '_wp_mf_page_template', $_POST['page_template']);
    }
    

    Third code:

    //MF Meta box for select template
    function mf_metabox_template () {
    global $post;
    
    if ( 0 != count( get_page_templates() ) ) {
    
      $template = get_post_meta($post->ID, '_wp_mf_page_template', TRUE);
      $template =  ($template != '') ? $template : false;
    ?>
      <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
      <option value='default'><?php _e('Default Template'); ?></option>
      <?php page_template_dropdown($template); ?>
      </select>
    <?php  
    }
    }
    
  2. You can create meta box using this code. Please paste this code in your function.php.

    /*********************custom meta box********************************/
    add_action( 'add_meta_boxes', 'add_events_metaboxes' );
    function add_events_metaboxes() {
      add_meta_box('wpt_school_location', 'Business Listing Data:','wpt_school_location', 'businesses', 'side', 'default');
    }
    //add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args ); 
    
    // Add the Events Meta Boxes
    // The Event Location Metabox
    
    function wpt_school_location() {
      global $post;
    
      // Noncename needed to verify where the data originated
      echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
      wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    
      // Get the location data if its already been entered
      $email = get_post_meta($post->ID, '_location', true);
      $tel = get_post_meta($post->ID, '_location1', true);
      $web = get_post_meta($post->ID, '_location2', true);
      $city = get_post_meta($post->ID, '_location3', true);
    
      // Echo out the field
      echo '<div class="heading">Address</div>';
      echo '<input type="text" name="_location" value="' . $email  . '" class="widefat" />';
      echo '<div class="heading">Telephone</div>';
      echo '<input type="text" name="_location1" value="' . $tel  . '" class="widefat" />';
      echo '<div class="heading">E-mail</div>';
      echo '<input type="text" name="_location2" value="' . $web  . '" class="widefat" />';
      echo '<div class="heading">Web</div>';
      echo '<input type="text" name="_location3" value="' . $city  . '" class="widefat" />';
    }
    
    // Save the Metabox Data
    function wpt_save_events_meta($post_id, $post) {
      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times
      if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
      }
    
      // Is the user allowed to edit the post or page?
      if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    
      // OK, we're authenticated: we need to find and save the data
      // We'll put it into an array to make it easier to loop though.
    
      $events_meta['_location'] = $_POST['_location'];
      $events_meta['_location1'] = $_POST['_location1'];
      $events_meta['_location2'] = $_POST['_location2'];
      $events_meta['_location3'] = $_POST['_location3'];
    
      // Add values of $events_meta as custom fields
    
      foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
      }
    }
    add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields
    // Add the Events Meta Boxes