放置自定义字段而不使用自定义字段

时间:2014-10-13 07:30:21

标签: php wordpress wordpress-plugin

我只需要在帖子上添加额外的字段,但不使用高级自定义字段。

请问我应该在我的function.php中进行哪些更改

我在下面的代码中做了以下代码,我需要额外的字段,其名称应为" company"我在下面的代码中需要进行哪些更改。

请有人能告诉我吗?

add_action( 'init', 'client' );
   function client() {
    register_post_type( 'client',
        array(
            'labels' => array(
                'name' => __( 'Our Client' ),
                'singular_name' => __( 'client' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'client'),
            'supports' => array( 'title','thumbnail')
        )
      );
  }

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

add_action( 'init', 'client' );
       function client() {
        register_post_type( 'client',
            array(
                'labels' => array(
                    'name' => __( 'Our Client' ),
                    'singular_name' => __( 'client' )
                ),
                'public' => true,
                'has_archive' => true,
                'rewrite' => array('slug' => 'client'),
                'supports' => array( 'title','thumbnail'),
                'register_meta_box_cb' => 'add_client_metaboxes',
            )
          );
      }

//Code for meta box.
add_action( 'add_meta_boxes', 'add_client_metaboxes' );

    function add_client_metaboxes() {
        add_meta_box('company_description', 'Company', 'company_description', 'client');
    }

    function company_description() {
        global $post;
     // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="itemmeta_noncename" id="itemmeta_noncename" value="' .
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

     // Get the location data if its already been entered

        $comapny_detail = get_post_meta($post->ID, 'comapny_detail', true);
     // Echo out the field
     echo '<input type="text" name="comapny_detail" value="'.$comapny_detail.'">';
    }

<强>被修改

    function wpt_client() {
        register_post_type( 'client',
             array(
                'labels' => array(
                    'name' => __( 'Clients' ),
                    'singular_name' => __( 'Client' ),
                    'add_new' => __( 'Add New Client' ),
                    'add_new_item' => __( 'Add New Client' ),
                    'edit_item' => __( 'Edit Client' ),
                    'new_item' => __( 'Add New Client' ),
                    'view_item' => __( 'View Client' ),
                    'search_items' => __( 'Search Client' ),
                    'not_found' => __( 'No cleint found' ),
                    'not_found_in_trash' => __( 'No client found in trash' )
                ),
                'public' => true,
                'supports' => array( 'title','editor','thumbnail'),
                'capability_type' => 'post',
                'rewrite' => array("slug" => "client"), // Permalinks format
                'menu_position' => 20,
                'register_meta_box_cb' => 'create_meta_boxes',
            )
        );
    }
    add_action( 'init', 'wpt_client' );
    /* Custom meta boxes  */
    add_action( 'add_meta_boxes', 'create_meta_boxes' );

    function create_meta_boxes() {
        add_meta_box( 'my-meta-box-id', __('Company Name'), 'client_info', 'client', 'normal', 'low' );
    }

    // Create meta box: Company Name
    function client_info( $post ) {
        $values = get_post_custom( $post->ID );
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <?php $text = get_post_meta($post->ID, 'client_info', true); ?>
    <input type="text" name="client_info" id="client_info" style="width: 100%; margin: 6px 0;" value="<?php echo $text; ?>" />
    <?php   
    }
    // Save meta box: Company Name
    add_action( 'save_post', 'save_client_info' );
    function save_client_info( $post_id ) {
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
        if( !current_user_can( 'edit_post' ) ) return;
        $allowed = array( 
            'a' => array( // on allow a tags
                'href' => array() // and those anchords can only have href attribute
            )
        );
        if( isset( $_POST['client_info'] ) )
            update_post_meta( $post_id, 'client_info', wp_kses( $_POST['client_info'], $allowed ) );
    }