在 WordPress 中将自定义字段添加到自定义帖子类型

问题描述 投票:0回答:3

我创建了几个自定义帖子类型。对于其中之一,我还想添加一个自定义字段。它应该只是一个简单的文本字段,您可以在其中输入一些文本。与标题字段类似。你会怎么做?我不想使用插件。

当前代码(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')
      )
    );
php wordpress
3个回答
5
投票

您需要创建自定义元框并在元框中添加该字段。

创建元框

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/,这是非常好的链接,它将帮助您创建自定义元框和一步一步的领域


1
投票

我知道这已经太晚了,但是在您的功能下,您需要将“自定义字段”添加到您的支持中:

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

0
投票

如果要创建自定义字段,则必须添加元框。 请参阅我的示例,它会对您有所帮助。

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' ); 

?>
© www.soinside.com 2019 - 2024. All rights reserved.