wordpress新增post_type和post_meta
自定义文章类型,增加post_type:
1:调用add_action('init','自定义方法名')就是在系统调用do_action('init')的时候执行自定义方法
2:编写自定义方法:
function my_custom_init() { $labels = array ( //用来配置文章类型显示在后台界面的一些描述性文字。默认为空 'name' => '书本name' , 'singular_name' => '书本singularname' , 'add_new' => 'Add_new' , 'add_new_item' => 'add_new_item' , 'edit_item' => 'edit_item' , 'new_item' => 'new_item' , 'view_item' => 'view_item' , 'search_items' => 'search_items' , 'not_found' => 'not_found' , 'not_found_in_trash' => 'not_found_in_trash' , 'parent_item_colon' => '' , 'menu_name' => 'menu_name' ); $args = array ( 'labels' => $labels , 'public' => true, //用于定义publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search的值 'publicly_queryable' => true, //可以从前台获取的变量( 'show_ui' => true, //是否生成一个默认的管理页面,也就是是否在后台有管理页面 'show_in_menu' => true, //是否在后台菜单项中显示,如果为ture,那么show_ui的值也必须设置为true,将会有一个顶级菜单项。还可以为一个字符串 'query_var' => true, //url重写会用到 'rewrite' => true, //是否有url重写,设置为false的话将会防止url重写 'capability_type' => 'post' , //查看、编辑、删除的能力类型 'has_archive' => true, //文章是否有归档,就是一个所有文章归档页面。 'hierarchical' => false, //文章是否有层级关系,也就是是否允许有父级文章 'menu_position' => null, //后台菜单中的位置 'supports' => array ( 'title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'comments' ) ); //对文章类型的一些功能支持 及时post表的一些属性 register_post_type( '需要定义的post_type' , $args ); / } 当然在实际工作中用不到这么的属性: //eg add_action( 'init' , 'ts_post_type_slider' ); function ts_post_type_slider() { register_post_type( 'slider' , array ( 'label' => __( 'Slider' ), 'public' => true, 'show_ui' => true, 'show_in_nav_menus' => false, 'menu_position' => 5, 'supports' => array ( 'title' , 'excerpt' , 'thumbnail' ) ) ); }3:到了这里差不多可以为文章增加新的post_type了,但是增加的post_type的supports可能不能满足你的需求,这个需要增加post_meta,就是所谓的自定义字段:
1:首先还是调用add_action('admin_menu', 'mytheme_add_box');看到这里发现给wordpress增加新东西都是调用add_action()的方法,第一个参数需要到wordpress官方网站查找。
2:编写mytheme_add_box方法.在mytheme_add_box方法中调用
add_meta_box($meta_box['id'], $meta_box['title'], $meta_box['showbox'], $meta_box['page'], $meta_box['context'], $meta_box['priority']);
$id HTML 代码中设置区域中id属性的值
$title 区域中的标题名称
$callback 添加的设置区域的显示函数(回调函数) 在回调函数中可以访问$post
$post_type 在 post 还是 page 的编辑页面中显示 ,也可以添加自定义的post_type
$context 设置区域的显示位置,主编辑区、边栏、其他('normal','advanced',或者 'side')
$priority 设置区域显示的优先级
$callback_args 回调函数接受的附加参数...
3:编写add_meta_box方法中的回调函数:
function slider_show_box() { global $meta_boxes , $post ; //感觉你点击新建post的时候,wordpress已经在数据库插入一条数据了,这个post的ID就也可以使用了 // Use nonce for verification echo '' ; echo mytheme_create_metabox( $meta_boxes [0]); }4:保存增加的post_meta了
add_action( 'save_post' , 'mytheme_save_data' ); function mytheme_save_data( $post_id ) { global $meta_boxes ; //验证 安全问题 if (isset( $_POST [ 'mytheme_meta_box_nonce' ])){ if (!wp_verify_nonce( $_POST [ 'mytheme_meta_box_nonce' ], basename ( __FILE__ ))) { return $post_id ; } } //是否是自动保存 if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) { return $post_id ; } // 权限检查 if ( 'page' == isset( $_POST [ 'post_type' ])) { if (!current_user_can( 'edit_page' , $post_id )) { return $post_id ; } } elseif (!current_user_can( 'edit_post' , $post_id )) { return $post_id ; } //保存,或者更新post_meta foreach ( $meta_boxes as $meta_box ){ foreach ( $meta_box [ 'fields' ] as $field ) { $old = get_post_meta( $post_id , $field [ 'id' ], true); $new = (isset( $_POST [ $field [ 'id' ]]))? $_POST [ $field [ 'id' ]] : "" ; if ( $new && $new != $old ) { update_post_meta( $post_id , $field [ 'id' ], $new ); } elseif ( '' == $new && $old ) { delete_post_meta( $post_id , $field [ 'id' ], $old ); } } } }查看更多关于wordpress新增post_type和post_meta - WordPress的详细内容...