WordPress CMS - 可能会使'动态'自定义帖子类型?
我有一个主要用作CMS的wordpress项目。感谢这个美妙的社区,我已经从对WordPress的一无所知到能够创建自定义帖子类型(用于我的产品列表),使用分层分类法对它们进行分类,并为此产品帖子类型创建自定义字段。WordPress CMS - 可能会使'动态'自定义帖子类型?
现在,我的产品非常简单。他们有ProductName
,ProductType
,ProductCategory
,DocumentName
和DocumentURL
。后两个是相关的,因为DocumentURL
是指向网络上PDF的链接,而DocumentName
是DocumentURL的标签。我为我的DocumentName
和DocumentURL
创建了自定义字段,并且可以为每个Product
自定义帖子添加1个。但是,我的Product
可能有很多文档URL和文档名称,或者它可能有1个,甚至是0个。有没有办法让它变成动态的,因此无论我有多少?或者,我是否需要提供最大数量并为产品自定义帖子创建许多自定义字段?
如果这只是直的PHP或ASP.NET我只是为Document元素创建一个单独的数据库表,并将一个好的表单与一些以2或3个文档字段开头的jQuery组合在一起,如果他们需要更多的可以单击+
符号添加另一行文档字段(如此http://deepliquid.com/projects/appendo/demos.php)。然后循环遍历它们并将它们添加到数据库中。这样的事情可以用WordPress吗?
我唯一的想法是为文档创建一个新的自定义文章类型并创建与他们的产品的关系,但是我无法围绕如何工作。
任何意见将不胜感激!谢谢!
我会假设你对PHP感到满意,因为你在这里说了什么。好消息是您的PHP知识将在这里派上用场。是时候学习register_post_type函数的新功能了;即register_meta_box_cb
参数。这使您可以定义一个函数来调用以在cpt添加和编辑页面上添加元框。举例来说,这里是一个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为您定义了一个元框,并提供了一个在metabox内创建内容的功能。在这种情况下,我们的代码指的是功能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应用程序中一样。我不会去做如何做到这一点,因为我会假设你可以做到这一点。实际上,我已经创建了一些这样的metabox,并且知道我发布的函数之后,我可以依靠我的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
函数将反序列化数组以供数据输出使用。
祝你好运!
哇谢谢你!让我一步一步阅读。再次感谢! – drpcken 2011-04-29 16:18:17
所以这些元框没有连接到任何自定义字段吗?我将不得不创建我自己的表为metaboxes的值并将它们链接到帖子ID? – drpcken 2011-04-29 17:05:48
哦,等等,我想我明白了,它是动态添加自定义元框!非常好!我会与此合作。谢谢! – drpcken 2011-04-29 18:18:12