个性化阅读
专注于IT技术分析

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

点击下载

有谁知道我如何实现以下目标:

  • 我有一个名为”项目”的自定义帖子类型, 我有5种不同的页面布局(每个页面都有不同数量的图像, 这些图像的大小不同, 排列方式也不同)。我认为, 如果我可以识别图像, 则可以使用CSS定位它们。如果每个布局都有其自己的模板页面, 其中包含用于所需图像的容器, 并且每个布局还具有用于放置图像的CSS。
  • 我想知道是否可以在帖子编辑器视图的右侧添加模板选择, 就像你可以通过这种方式为页面使用一样, 可以为每个帖子分配模板(从1-5)。
  • 我在主帖子编辑器中为图像上载字段和其他数据添加了自定义元框, 但理想情况下, 其他页面仅应显示所需的上载字段时, 应该对此进行更改。

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

一如既往的感谢任何帮助。谢谢


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

#2


你可以使用此代码创建元框。请将此代码粘贴到你的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
赞(0)
未经允许不得转载:srcmini » 有人知道如何在WordPress中将元框模板选择添加到自定义帖子类型吗?

评论 抢沙发

评论前必须登录!