WordPress CMS -可以使'动态'自定义帖子类型


WordPress CMS - Possible to make 'dynamic' custom post types?

我有一个wordpress项目,将主要用作CMS。感谢这个美妙的社区,我从对WordPress一无所知到能够创建自定义帖子类型(为我的产品列表),使用分层分类法对它们进行分类,并为这个产品帖子类型创建自定义字段。

现在,我的产品非常简单。它们有ProductName, ProductType, ProductCategory, DocumentNameDocumentURL。最后两个是相关的,因为DocumentURL是到web上PDF的链接,而DocumentName是DocumentURL的标签。我已经为我的DocumentNameDocumentURL创建了自定义字段,并且可以为每个Product自定义帖子添加1个字段。然而,我的Product可以有许多文档URL和文档名称,或者它可以有1,甚至0。有没有办法让它变成动态的,不管我有多少个?或者我是否需要提出一个最大值并为Product自定义帖子创建那么多自定义字段?

如果这只是直接PHP或ASP。我会为文档元素创建一个单独的db表,并与一些jQuery一起创建一个漂亮的表单,以2或3个文档字段开始,如果他们需要更多,他们可以单击+符号来添加另一行文档字段(如http://deepliquid.com/projects/appendo/demos.php)。然后循环遍历它们并将它们添加到数据库中。这样的事情可能与WordPress?

我唯一的其他想法是为文档创建一个新的自定义帖子类型,并创建一个与他们的产品的关系,但我不明白这是如何工作的。

任何建议将不胜感激!谢谢你!

我假设您已经熟悉PHP了。好消息是您的PHP知识在这里会派上用场。是时候学习register_post_type函数的新特性了;即register_meta_box_cb参数。这允许您定义一个函数,以便在cpt添加和编辑页面上添加一个metabox。例如,下面是一个cpt(直接取自抄本),添加了参数:

add_action('init', 'codex_custom_init');
function codex_custom_init() 
{
  $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Books'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'register_meta_box_cb' => 'my_meta_box_function',
    'supports' => array('title','editor','author','thumbnail','excerpt','comments')
  ); 
  register_post_type('book',$args);
}}

现在你已经定义了元框cb函数,像这样使用它:

function my_beta_box_function(){
    add_meta_box(
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ), 
        'myplugin_inner_custom_box',
        'book'
    );
}

add_meta_box为您定义了一个元框,并提供了一个在元框中创建内容的函数。在本例中,我们的代码引用了函数myplugin_inner_custom_box。有关添加元框的更多信息,请访问http://codex.wordpress.org/Function_Reference/add_meta_box。因此,我们现在需要通过定义函数来添加内容:

function myplugin_inner_custom_box()
{
   // Everything here would appear in the meta box
}
此时,您可以利用PHP知识创建一个可以在提交后处理的HTML表单。您可以添加+按钮并使用PHP,就像在任何其他PHP应用程序中一样。我就不讲怎么做了因为我假设你们能做。我实际上已经创建了一些像这样的代谢库,在知道我发布的函数之后,我可以依靠我的PHP知识。最后一部分是保存输入。通过使用定义保存例程和save_post操作的函数,可以使用PHP知识保存数据:
/* Do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return $post_id;
  // 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['myplugin_noncename'], plugin_basename(__FILE__) ) )
      return $post_id;

  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }
  // OK, we're authenticated: we need to find and save the data
  $mydata = $_POST['myplugin_new_field'];
  update_post_meta('my_field', $mydata);
   return $mydata;
}

最后,关于保存函数的说明。如果你用一个数组值来命名你的输入字段(例如,docs[]), $_POST['docs']的值将是一个数组。幸运的是,WP的update_post_meta函数都设置为处理数组输入。它会序列化它并输入它。使用兼容的get_post_meta函数将反序列化数组,以便在数据输出时使用。

祝你好运!