有人知道如何在Wordpress中添加元框模板选择到自定义帖子类型吗?

时间:2013-02-24 14:18:14

标签: templates wordpress-theming custom-post-type wordpress

是否有人知道如何实现以下目标:

  • 我有一个名为'projects'的自定义帖子类型我有5个不同的页面 布局(每个都有不同大小的不同数量的图像 不同的安排)。我想如果我可以识别图像我 可以使用CSS定位它们。如果每个布局都有自己的模板页面 用于所需图像的容器,每个也有CSS 定位图像。
  • 我想知道是否可以添加一个 模板选择在帖子编辑器视图的右侧,就像你一样 以这种方式可用于页面,可以分配模板(来自 1-5)每个帖子。
  • 我正在为图片上传字段添加自定义元框 和主帖子编辑器中的其他数据,但理想情况下这应该 更改其他页面只显示所需的上传 字段。

任何想法?我确实考虑为每个布局(5个模板)设置自定义帖子类型,允许您在所需的布局类型下发布新项目,然后在页面上查询包含所有5个自定义后期类型(布局)的循环。我担心这可能会很麻烦,因为要查找以后编辑的项目,您需要知道它们属于哪个模板页面(自定义后期类型),但更重要的是,项目的URL不会是www.sitename.co。英国/项目,但改为www.sitename.co.uk/custom-post-type1,www.sitename.co.uk/custom-post-type2 ???不是吗?

任何帮助都一如既往地真正受到赞赏。谢谢

2 个答案:

答案 0 :(得分:1)

这是我到目前为止已经确定的代码,任何想法如何自定义?

这应该在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;

第二段代码:

// 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']);
}

第三个代码:

//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  
}
}

答案 1 :(得分:0)

您可以使用此代码创建元框。请将此代码粘贴到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