自定義文章類型,增加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方法中調用
$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);
- }
- }
- }
- }
新聞熱點
疑難解答
圖片精選