2014-03-12 39 views
3

我添加了多个自定义帖子类型到WordPress网站,我试图通过使用变量和合并函数来优化代码。我能够发送2个变量到用于register_post_typecreate_rma_project_post_type函数。我想用附加metabox创建函数的参数来做同样的事情。下面的第一个代码完全工作。它采用register_meta_box_cb调用add_project_metaboxesWordPress的:自定义帖子类型,发送数据到“register_meta_box_cb”Arg

add_action('init', create_rma_project_post_type('project','our-people')); 
function create_rma_project_post_type($post_type,$display_page) { 
    $prefix = 'rma'; 
    $post_label = ucfirst($post_type); 

    register_post_type($prefix.'_'.$post_type, 
     array(
      'labels' => array(
       'name' => __($post_label.'s'), 
       'singular_name' => __($post_label), 
       'all_items' => __($post_label.' List'), 
       'add_new' => __('Add New '.$post_label), 
       'add_new_item' => __('Add New '.$post_label), 
       'edit_item' => __('Edit '.$post_label), 
       'new_item' => __('New '.$post_label), 
       'view_item' => __('View '.$post_label), 
       'search_items' => __('Search'), 
       'parent_item_colon' => 'about-us/'.$display_page, 
      ), 
      'public' => true, 
      'has_archive' => false, 
      'rewrite' => array('slug' => 'about-us/'.$display_page,'with_front' => false), 
      'supports' => array('title','editor','thumbnail'), 
      'register_meta_box_cb' => 'add_project_metaboxes', 
     ) 
    ); 
} 

function add_project_metaboxes() { 
    add_meta_box('rma_project_metabox_information', 'Project Information', 'rma_project_metabox_information_callback', 'rma_project', 'normal', 'default'); 
} 

我想要做的就是改变metabox呼叫发送变化的,即能够从功能中删除“工程”二字的目标,并使用一个函数用于所有自定义帖子类型。

这是希望该行我读:

  'register_meta_box_cb' => add_project_metaboxes($post_type), 

当我做到这一点,这是行不通的。这些都没有这些:

  'register_meta_box_cb' => 'add_project_metaboxes($post_type)', 
      'register_meta_box_cb' => function(add_project_metaboxes($post_type)), 
      'register_meta_box_cb' => function('add_project_metaboxes($post_type)'), 
      'register_meta_box_cb' => sprintf(__('add_project_metaboxes(%s)'), $post_type), 

我的第一个问题是,这是可能目前与WordPress?如果是的话,我该怎么做?感谢任何帮助,如果你需要澄清任何事情,请让我知道。


编辑2017年

使用这个和其他的答案我创建了一组辅助类,使CTP的更易于管理。如果你想看到成品,请参阅:

https://gist.github.com/Kelderic/dc641aced67f0c0cb0a4a1ded17fa0d4

回答

3

register_meta_box_cb回调函数有一个输入参数,即当前编辑的帖子的WP_Post对象。

所以我认为你应该能够通过使用get_post_type() WordPress功能从该对象中获得帖子类型。

如果为register_post_type()检查出source code你会发现,它包含此部分:

if ($args->register_meta_box_cb) 
    add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1); 

所以试图取代:

'register_meta_box_cb' => 'add_project_metaboxes', 

有:

'register_meta_box_cb' => 'custom_add_metaboxes', 

其中:

function custom_add_metaboxes($post) 
{ 

    // get the current post type 
    $post_type = get_post_type($post); 

    // your logic for the add_meta_box code 
    // ... 

} 

包含基于当前帖子类型的逻辑。

更新:

是存储/数据数组的自定义后类型相关联的方法?

一个)是的,则可以使用update_post_meta()存储与关联于自定义后类型数据的给定$post_id

update_post_meta($post_id, $meta_key, $meta_value); 

其中$meta_value可以是阵列。例如:

update_post_meta(38, 'my_data', array('somekey' => 'somevalue')); 

要获取数组数据,你可以使用get_post_meta()

$data = get_post_meta(38, 'my_data', FALSE); 

B)如果你想存储一些数据阵列,这是关系到所有文章在给定的定制帖子类型,您可以使用update_option()来存储它,并使用get_option()来获取它。

例如,你可以使用:

$data = array( 
       'cpt1' => array('somekey1' => 'somevalue1'), 
       'cpt2' => array('somekey2' => 'somevalue2'), 
); 

update_option('my_data', $data); 

存储$data阵列和

$data = get_option('my_data'); 

给它取回来。

-

希望得到这个帮助。

+0

美丽!我没有意识到'register_meta_box_cb'调用的函数会自动发送'$ post'。通过任何机会让你知道是否有方法将数据数组存储/关联到自定义帖子类型? –

+0

@AndyM我更新了答案,并举例说明了如何将数据数组存储/关联到自定义帖子类型。希望这个帮助。 – birgire

+0

看起来不错,我会在3小时内给你赏金。 –

1

它不是一个真正的WordPress的问题,更多的是如何做的问题得到的功能在存储时叫什么名字?如果它正在寻找一个函数名称,你需要像这样给它。

我明白你要做什么,如果你想在声明数组之后调用add_project_metaboxes($ post_type),那么add_meta_boxes会被挂钩到“init”,所以它可能会工作。顺便说一句,上面存在的函数将不起作用,除非您修改它以获取参数。

如果不是OOP类型的对象可能是你正在寻找。

+0

嗯,是的,没有。这是一个问题,WordPress如何专门调用存储的函数。尽管如此,可能只是在'create_rma_project_post_type'内部调用'add_project_metaboxes()'。我会看一看。你能否澄清第二段的最后一句话,因为第一段代码中的所有内容都按原样运行,所以我不确定你的意思。 –

+0

是的,如果你调用add_project_metaboxes()会工作,但如果你尝试调用add_project_metaboxes($ var),你需要修改函数来获取一个变量。但你的问题无论如何都是这样做的! – David

+0

哦,是的。我需要将'“project”'的所有实例更改为'。$ post_type.',并且放入一个参数来捕获发送的数据。这部分不是问题。 –

1

即时通讯发布一个基类我用于自定义帖子类型,它不是我的代码,我不记得我在哪里拿起它,但信贷给创造者!它对我通常创建自定义帖子非常有效,它应该与您的代码一起工作(请参阅下文,我试图快速重现您的功能)。希望它有帮助!

btw:您可以通过使用post_meta来存储帖子的自定义信息(您可以使用postid来拉取此信息)。让我知道如果这不清楚,生病在pastebin做些什么。

class base_posttype { 

public $post_type_name; 
public $post_type_variables; 
public $post_type_labels; 
public $meta_keys; 
public $result= array(); 

public function __construct($name, $variables= array(), $labels=array()) { 

    $this->post_type_name = $name ; 
    $this->post_type_variables = $variables; 
    $this->post_type_labels = $labels; 


    if(! post_type_exists ($this-> post_type_name)){ 

     add_action ('init', array(&$this, 'register_post_type')); 

    } 

    $this->save(); 

} //------------end construct------------------------------------------ 


public function register_post_type() { 

    $name  = ucwords(str_replace('_', ' ', $this->post_type_name)); 
    $plural  = $name . 's'; 

    $labels = array_merge(

    // Default 
    array(
     'name'     => _x($plural, 'post type general name'), 
     'singular_name'   => _x($name, 'post type singular name'), 
     'add_new'    => _x('Add New', strtolower($name)), 
     'add_new_item'   => __('Add New ' . $name), 
     'edit_item'    => __('Edit ' . $name), 
     'new_item'    => __('New ' . $name), 
     'all_items'    => __('All ' . $plural), 
     'view_item'    => __('View ' . $name), 
     'search_items'   => __('Search ' . $plural), 
     'not_found'    => __('No ' . strtolower($plural) . ' found'), 
     'not_found_in_trash' => __('No ' . strtolower($plural) . ' found in Trash'), 
     'parent_item_colon'  => '', 
     'menu_name'    => $plural 
    ), 


    $this->post_type_labels 

    ); 


    $args = array_merge(


    array(
     'label'     => $plural, 
     'labels'    => $labels, 
     'public'    => true, 
     'show_ui'    => true, 
     'supports'    => array('title', 'editor'), 
     'show_in_nav_menus'  => true, 
     '_builtin'    => false, 
    ), 

    // Given args 
    $this->post_type_args 

    ); 

    // Register the post type 
    $this->result[]=register_post_type($this->post_type_name, $args);  
} 


/* attach the taxonomy to the post type */ 
public function add_taxonomy($name, $args = array(), $labels = array()) { 

    if(! empty($name)) { 
    $post_type_name = $this->post_type_name; 

    // Taxonomy properties 
    $taxonomy_name  = strtolower(str_replace(' ', '_', $name)); 
    $taxonomy_labels = $labels; 
    $taxonomy_args  = $args; 

    if(! taxonomy_exists($taxonomy_name)) { 

    //Capitilize the words and make it plural 
    $name  = ucwords(str_replace('_', ' ', $name)); 
    $plural  = $name . 's'; 

    // Default labels, overwrite them with the given labels. 
    $labels = array_merge(

    // Default 
     array(
      'name'     => _x($plural, 'taxonomy general name'), 
      'singular_name'   => _x($name, 'taxonomy singular name'), 
      'search_items'   => __('Search ' . $plural), 
      'all_items'    => __('All ' . $plural), 
      'parent_item'   => __('Parent ' . $name), 
      'parent_item_colon'  => __('Parent ' . $name . ':'), 
      'edit_item'    => __('Edit ' . $name), 
      'update_item'   => __('Update ' . $name), 
      'add_new_item'   => __('Add New ' . $name), 
      'new_item_name'   => __('New ' . $name . ' Name'), 
      'menu_name'    => __($name), 
    ), 

     // Given labels 
     $taxonomy_labels 

    ); 

    // Default arguments, overwritten with the given arguments 
    $args = array_merge(

    // Default 
    array(
     'label'     => $plural, 
     'labels'    => $labels, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_nav_menus'  => true, 
     '_builtin'    => false, 
    ), 

    // Given 
    $taxonomy_args 

    ); 

    // Add the taxonomy to the post type 
    add_action('init', function() use($taxonomy_name, $post_type_name, $args) { 
     register_taxonomy($taxonomy_name, $post_type_name, $args); 
     }); 

    } else { 

     add_action('init', function() use($taxonomy_name, $post_type_name) { 
      register_taxonomy_for_object_type($taxonomy_name, $post_type_name); 
     }); 

    } 


    } 
} 

/* Attaches meta boxes to the post type */ 
public function add_meta_box($title, $fields = array(), $context = 'normal', $priority = 'default') { 

    // We need to know the Post Type name again 
    $post_type_name = $this->post_type_name; 

    // Meta variables 
    $box_id   = strtolower(str_replace(' ', '_', $title)); 
    $box_title  = ucwords(str_replace('_', ' ', $title)); 
    $box_context = $context; 
    $box_priority = $priority; 

    // Make the fields global 
    global $custom_fields; 
    $custom_fields[$title] = $fields; 

    add_action('admin_init', function() use($box_id, $box_title, $post_type_name, $box_context, $box_priority, $fields) { 

    add_meta_box($box_id, $box_title, function($post, $data) { 
      global $post; 

      // Nonce field for some validation 
      wp_nonce_field(plugin_basename(__FILE__), 'custom_post_type'); 

      // Get all inputs from $data 
      $custom_fields = $data['args'][0]; 

      // Get the saved values 
      $meta = get_post_custom($post->ID); 

      // Check the array and loop through it 
      if(! empty($custom_fields)) { 
       /* Loop through $custom_fields */ 
       foreach($custom_fields as $label => $type) { 
        $field_id_name = strtolower(str_replace(' ', '_', $data['id'])) . '_' . strtolower(str_replace(' ', '_', $label)); 

        echo '<label for="' . $field_id_name . '">' . $label . '</label><input type="text" name="custom_meta[' . $field_id_name . ']" id="' . $field_id_name . '" value="' . $meta[$field_id_name][0] . '" />'; 
       } 
      }}, 
      $post_type_name, 
      $box_context, 
      $box_priority, 
      array($fields) 
      ); 
     }); 

} 



/* Listens for when the post type being saved */ 
public function save() { 

     // Need the post type name again 
$post_type_name = $this->post_type_name; 

add_action('save_post', function() use($post_type_name) { 
     // Deny the WordPress autosave function 
     if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

     if (! wp_verify_nonce($_POST['custom_post_type'], plugin_basename(__FILE__))) return; 

     global $post; 

     if(isset($_POST) && isset($post->ID) && get_post_type($post->ID) == $post_type_name) 
     { 
      global $custom_fields; 

      // Loop through each meta box 
      foreach($custom_fields as $title => $fields) 
      { 
       // Loop through all fields 
       foreach($fields as $label => $type) 
       { 
        $field_id_name = strtolower(str_replace(' ', '_', $title)) . '_' . strtolower(str_replace(' ', '_', $label)); 

        update_post_meta($post->ID, $field_id_name, $_POST['custom_meta'][$field_id_name]); 
       } 

      } 
     } 
    }); 

} 
} 

然后您可以使用此类创建帖子,例如

class project_post_type { 

public function __construct() { 


$project= new base_posttype ('project', array(
'has_archive' => TRUE, 
     'menu_position' => 10, 
     'rewrite' => array(
      'slug' => 'our-people', 
      'with_front' => FALSE, 
     ), 
     'supports' => array('title', 'editor', 'thumbnail', 'comments', 'custom-fields', 'revisions'), 
     'menu_icon' => AD_URL . '/resources/img/adlogo.png' 
    )); 

    $project->add_taxonomy('location'); 
$project->add_taxonomy ('categories', array(
'hierarchical' => true 
)); 

$project->add_meta_box('Project Info' , array(
    'price' => 'text', 
    'condition' => 'text', 
    'age' => 'text', 
    'expiration_date' => 'text', // int 
    'fee' => 'text', 
    'discount' => 'text', 
    'due' => 'text', 
    'paid' => 'text', 
    'sold' => 'text' //boolan 
    ) 
); 

} 
} 

$project= new project_post_type; 

通常,如果我有数据的阵列我需要使用为后型,i设定它的类内,所以一个方法拉元密钥和一个方法来保存,大部分时间不必要的,但在有些情况下,我喜欢从前端提交表单,它派上用场。

+0

这看起来很有趣,但不像birgire的答案那样直接适用。我很欣赏帮助,所以+1! –

相关问题