如何使用类

时间:2015-06-20 18:48:58

标签: wordpress custom-post-type

我的插件中有2个文件,其中一个是自定义帖子类型及其元框和元框的字段。第二个文件打印出元框,基本上是布局。

我能够成功保存文本字段和复选框的数据并选择列表。然而,对于我的生活,我无法弄清楚如何添加广播和网址字段,以及其他类型的字段可能稍微复杂一点,我坚持3个字段(文本,选择,复选框)。

有人能帮助我吗?是模板和我创建这个的方式好吗?我搜索了很多教程,一些使用元框和元字段,一些使用类,其他人不使用类,它如此令人困惑,我不太确定如果我这样做最好的方式。

我的第一个档案:

<?php
/**
         * The Constructor
         */
        public function __construct()
        {
            // register actions
            add_action('init', array(&$this, 'init'));
            add_action('admin_init', array(&$this, 'admin_init'));
        } // END public function __construct()

        /**
         * hook into WP's init action hook
         */
        public function init()
        {
            // Initialize Post Type
            $this->create_post_type();
            add_action('save_post', array(&$this, 'save_post'));
        } // END public function init()

        /**
         * Create the post type
         */
        public function create_post_type()
        {
            register_post_type(self::POST_TYPE,
                array(
                    'labels' => array(
                        'name' => __(sprintf('%ss', ucwords(str_replace("_", " ", self::POST_TYPE)))),
                        'singular_name' => __(ucwords(str_replace("_", " ", self::POST_TYPE))),
                    ),
                    'public' => true,
                    'has_archive' => true,
                    'publicly_queryable' => true,
                    'query_var' => true,
                    'rewrite' => true,
                    'capability_type' => 'post',
                    'description' => __("Artist post type for My Plugin"),
                    'show_ui' => true,
                    'supports' => array(
                        'title', 'editor', 'thumbnail', 'revisions',
                    ),
                    'menu_position' => 5,
                    // 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
                    // 'taxonomies' => array( 'category', 'post_tag' ), // add default post categories and tags
                )
            );
        }

        /**
         * Save the metaboxes for this custom post type
         */
        public function save_post($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;
            }

            if(isset($_POST['post_type']) && $_POST['post_type'] == self::POST_TYPE && current_user_can('edit_post', $post_id))
            {
                foreach($this->_meta as $field_name)
                {
                    // Update the post's meta field
                    update_post_meta($post_id, $field_name, $_POST[$field_name]);
                }
            }
            else
            {
                return;
            } // if($_POST['post_type'] == self::POST_TYPE && current_user_can('edit_post', $post_id))
        } // END public function save_post($post_id)

        /**
         * hook into WP's admin_init action hook
         */
        public function admin_init()
        {           
            // Add metaboxes
            add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));

        } // END public function admin_init()

        /**
         * hook into WP's add_meta_boxes action hook
         */
        public function add_meta_boxes()
        {
            // Add this metabox to every selected post
            add_meta_box( 
                sprintf('pr_my_plugin_%s_section', self::POST_TYPE),
                sprintf('%s Information', ucwords(str_replace("_", " ", self::POST_TYPE))),
                array(&$this, 'add_inner_meta_boxes'),
                self::POST_TYPE
            );                  
        } // END public function add_meta_boxes()

        /**
         * called off of the add meta box
         */     
        public function add_inner_meta_boxes($post)
        {       
            // Render the job order metabox
            include(sprintf("%s/../templates/%s_metabox.php", dirname(__FILE__), self::POST_TYPE));         
        } // END public function add_inner_meta_boxes($post)
    } // END class Artist_Template
} // END if(!class_exists('Artist_Template'))
?>

我的第二个档案

<?php
$values = get_post_custom( $post->ID );
$selected = isset( $values['meta_f'] ) ? esc_attr( $values['meta_f'][0] ) : ”;
$check = isset( $values['meta_g'] ) ? esc_attr( $values['meta_g'][0] ) : ”;
$radioselected = isset( $values['meta_h'] ) ? esc_attr( $values['meta_h'][0] ) : ”;
?>

<table> 
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_country" class="pr-myplugin-admin"><?php _e( 'Country', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="text" id="meta_country" name="meta_country" value="<?php echo @get_post_meta($post->ID, 'meta_country', true); ?>" />
        </td>
    </tr>
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_fburl" class="pr-myplugin-admin"><?php _e( 'Facebook URL', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="text_url" id="meta_fburl" name="meta_fburl" value="<?php echo @get_post_meta($post->ID, 'meta_fburl', true); ?>" />
        </td>
    </tr>                
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_twitterurl" class="pr-myplugin-admin"><?php _e( 'Twitter URL', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="text_url" id="meta_twitterurl" name="meta_twitterurl" value="<?php echo @get_post_meta($post->ID, 'meta_twitterurl', true); ?>" />
        </td>
    </tr>
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_website" class="pr-myplugin-admin"><?php _e( 'Website URL', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="text_url" id="meta_website" name="meta_website" value="<?php echo @get_post_meta($post->ID, 'meta_website', true); ?>" />
        </td>
    </tr>      
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_e" class="pr-myplugin-admin"><?php _e( 'Meta E', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="text" name="meta_e" id="meta_e" value="<?php echo @get_post_meta($post->ID, 'meta_e', true); ?>" />
        </td>
    </tr>
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_f" class="pr-myplugin-admin"><?php _e( 'Meta F', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <select name="meta_f" id="meta_f">
                <option value="red" <?php selected( $selected, 'red' ); ?>>Red</option>
                <option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option>
            </select>
        </td>
    </tr>
    <tr valign="top">
        <th class="metabox_label_column">
            <label for="meta_g" class="pr-myplugin-admin"><?php _e( 'Meta G', 'pr_my_plugin' )?></label>
        </th>
        <td>
            <input type="checkbox" id="meta_g" name="meta_g" <?php checked( $check, 'on' ); ?> />
        </td>
    </tr>   
</table>

0 个答案:

没有答案