簡體   English   中英

在Wordpress中將自定義字段添加到自定義帖子類型

[英]Adding custom field to custom post type in Wordpress

我創建了幾個自定義帖子類型。 我還想向其中一個添加自定義字段。 它應該只是一個簡單的文本字段,您可以在其中輸入一些文本。 類似於標題字段。 你會怎么做? 我不想使用插件。

當前代碼(functions.php)

    register_post_type( 'cases',
      array(
        'labels' => array(
            'name' => __( 'Cases' ),
            'singular_name' => __( 'Case' )
        ),
        'publicly_queryable' => true,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'cases'),
        'supports' => array('title','editor','thumbnail')
      )
    );

您需要創建自定義元框並將該字段添加到元框中。

創建元框

function add_your_fields_meta_box() {
add_meta_box(
    'your_fields_meta_box', // $id
    'Your Fields', // $title
    'show_your_fields_meta_box', // $callback
    'your_post', // $screen
    'normal', // $context
    'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_your_fields_meta_box' );

HTML部分

function show_your_fields_meta_box() {
global $post;  
    $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>

<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

<!-- All fields will go here -->

<?php }

在數據庫中保存字段

function save_your_fields_meta( $post_id ) {   
// verify nonce
if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
    return $post_id; 
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return $post_id;
}
// check permissions
if ( 'page' === $_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;
    }  
}

$old = get_post_meta( $post_id, 'your_fields', true );
$new = $_POST['your_fields'];

if ( $new && $new !== $old ) {
    update_post_meta( $post_id, 'your_fields', $new );
} elseif ( '' === $new && $old ) {
    delete_post_meta( $post_id, 'your_fields', $old );
}
}
add_action( 'save_post', 'save_your_fields_meta' );

有關更多詳細信息,您可以在此處查看https://www.taniarascia.com/wordpress-part-three-custom-fields-and-metaboxes/ ,這是一個非常不錯的鏈接,它將幫助您逐步創建自定義元框和字段步

如果要創建自定義字段,則必須添加元框。 請參閱我的示例,它將對您有所幫助。

function show_your_fields_meta_box() {
    global $post;  
        $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
// Just paste your input here as below.
        <input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
        <p>
            <label for="your_fields[text]">Input Text</label>
            <br>
            <input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="<?php echo $meta['text']; ?>">
        </p>


    <?php }


function save_your_fields_meta( $post_id ) {   
    // verify nonce
    if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
        return $post_id; 
    }
    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // check permissions
    if ( 'page' === $_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;
        }  
    }

    $old = get_post_meta( $post_id, 'your_fields', true );
    $new = $_POST['your_fields'];

    if ( $new && $new !== $old ) {
        update_post_meta( $post_id, 'your_fields', $new );
    } elseif ( '' === $new && $old ) {
        delete_post_meta( $post_id, 'your_fields', $old );
    }
}
add_action( 'save_post', 'save_your_fields_meta' ); 

?>

我知道這已經很晚了,但是在您的職責下,您需要在支持中添加“自定義字段”:

'supports' => array('title','editor','thumbnail', 'custom-fields'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM