Php WordPress CMS-可以创建“动态”自定义帖子类型吗?
我有一个wordpress项目,主要用作CMS。感谢这个奇妙的社区,我已经从对WordPress一无所知变成能够为我的产品列表创建自定义帖子类型,使用层次分类法对它们进行分类,并为这个产品帖子类型创建自定义字段 现在,我的产品非常简单。它们有一个ProductName、一个ProductType、一个ProductCategory、一个DocumentName和一个DocumentURL。最后两个是相关的,因为DocumentURL是指向web上PDF的链接,DocumentName是DocumentURL的标签。我已经为我的DocumentName和DocumentURL创建了自定义字段,并且可以在每个产品自定义帖子中添加其中的一个。但是,我的产品可能有许多文档URL和文档名称,也可能有1,甚至0。有没有一种方法可以让它充满活力,这样就不管我有多少?或者我需要为产品定制帖子设置一个最大值并创建那么多定制字段吗 如果这只是单纯的PHP或ASP.NET,我只需要为文档元素创建一个单独的db表,并将一个漂亮的表单与一些以2或3个文档字段开头的jQuery组合在一起,如果他们需要更多,他们可以单击+符号添加另一行文档字段,如下所示。然后循环遍历它们并将它们添加到数据库中。用WordPress可以做这样的事情吗 我唯一的另一个想法是为文档创建一个新的自定义post类型,并与它们的产品建立关系,但我不知道该如何工作Php WordPress CMS-可以创建“动态”自定义帖子类型吗?,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我有一个wordpress项目,主要用作CMS。感谢这个奇妙的社区,我已经从对WordPress一无所知变成能够为我的产品列表创建自定义帖子类型,使用层次分类法对它们进行分类,并为这个产品帖子类型创建自定义字段 现在,我的产品非常简单。它们有一个ProductName、一个ProductType、一个ProductCategory、一个DocumentName和一个DocumentURL。最后两个是相关的,因为DocumentURL是指向web上PDF的链接,DocumentName是Docum
任何建议都将不胜感激!谢谢大家! 根据您在这里所说的,我假设您对PHP感到满意。好消息是,您的PHP知识将在这里派上用场。是时候学习register\u post\u类型函数的新特性了;即寄存器\元\框\ 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);
}}
现在您已经定义了meta box cb函数,可以像以下那样使用它:
function my_beta_box_function(){
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'book'
);
}
添加元框为您定义了一个元框,并提供了一个在元框中创建内容的函数。在本例中,我们的代码引用函数myplugin\u internal\u custom\u box。查看更多关于添加元框的信息。因此,我们现在需要通过定义函数来添加内容:
function myplugin_inner_custom_box()
{
// Everything here would appear in the meta box
}
此时,您可以利用PHP知识创建一个HTML表单,该表单可以在提交后处理。您可以添加+按钮并像在任何其他PHP应用程序中一样使用PHP。我不想讨论如何做到这一点,因为我假设你能做到。实际上,我已经创建了一些这样的元数据库,在了解了我发布的函数之后,我可以依靠我的PHP知识。最后一点是保存输入。通过使用定义save例程和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;
}
最后,请注意save函数。如果使用数组值命名输入字段,例如docs[],则$\u POST['docs']值将是一个数组。幸运的是,WP的update_post_元函数已全部设置为处理数组输入。它将序列化它并输入它。使用兼容的get_post_元函数将取消阵列序列化,以备数据输出时使用
祝你好运 根据您在这里所说的,我假设您对PHP感到满意。好消息是,您的PHP知识将在这里派上用场。是时候学习register\u post\u类型函数的新特性了;即寄存器\元\框\ 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);
}}
现在您已经定义了meta box cb函数,可以像以下那样使用它:
function my_beta_box_function(){
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
'book'
);
}
添加元框为您定义了一个元框,并提供了一个在元框中创建内容的函数。在本例中,我们的代码引用函数myplugin\u internal\u custom\u box。查看更多关于添加元框的信息。因此,我们现在需要通过定义函数来添加内容:
function myplugin_inner_custom_box()
{
// Everything here would appear in the meta box
}
此时,您可以利用PHP知识创建一个HTML表单,该表单可以在提交后处理。您可以添加+按钮并像在任何其他PHP应用程序中一样使用PHP。我不想讨论如何做到这一点,因为我假设你能做到。实际上,我已经创建了一些这样的元数据库,在了解了我发布的函数之后,我可以依靠我的PHP知识。最后一点是保存输入。通过使用定义save例程和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;
}
最后,请注意save函数。如果使用数组值命名输入字段,例如docs[],则$\u POST['docs']值将为b
对数组进行加密。幸运的是,WP的update_post_元函数已全部设置为处理数组输入。它将序列化它并输入它。使用兼容的get_post_元函数将取消阵列序列化,以备数据输出时使用
祝你好运 哇,谢谢你!让我一步一步地读一遍。再次感谢!所以这些元盒没有连接到任何自定义字段,对吗?我必须为元框的值创建自己的表,并将它们链接到post ID?哦,等等,我想我明白了,它正在动态添加自定义元框!很不错的!我会处理这个。非常感谢。抱歉…现在当我看到自定义字段时,我并不总是想到WP自定义字段。我通常只想到可以作为post_meta提交到的字段。很抱歉给你带来了困惑。我希望一切顺利。没问题!你的回答和建议非常宝贵,这个项目现在已经接近完成。我不仅学到了很多关于wordpress的知识,还学到了PHP本身:再次感谢你!哇,谢谢你!让我一步一步地读一遍。再次感谢!所以这些元盒没有连接到任何自定义字段,对吗?我必须为元框的值创建自己的表,并将它们链接到post ID?哦,等等,我想我明白了,它正在动态添加自定义元框!很不错的!我会处理这个。非常感谢。抱歉…现在当我看到自定义字段时,我并不总是想到WP自定义字段。我通常只想到可以作为post_meta提交到的字段。很抱歉给你带来了困惑。我希望一切顺利。没问题!你的回答和建议非常宝贵,这个项目现在已经接近完成。我不仅学到了很多关于wordpress的知识,还学到了PHP本身:再次感谢你!