繁体   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