我想你在寻找“元盒子”。
您将与add_meta_box() 到create 一个或多个用于帖子类型的新元框。(Codex)
你必须设置一些callback function 其中包含要显示/显示的字段的HTML。
您还需要一个函数save 这些字段使用add_post_meta() 和update_post_meta(). (抄本至ADD 和UPDATE)
如果你想的话remove 可以使用的一些现有元框remove_meta_box(). (Codex)
<小时>
Some Details:
使用以下代码,您可以在“快速链接”帖子类型的新建/编辑屏幕上创建一个新的元框。(因为我输入了“quicklink”作为帖子类型)
function create_custom_metabox() {
    add_meta_box( 
        \'my_meta\', // HTML \'id\' attribute of the metabox
        __( \'My Setting\', \'textdomain\' ), // Title of metabox
        \'my_fields_callback\', // Function that prints out the HTML for metabox
        \'quicklink\', // The post-type of writing screen on which to show the edit screen section (example \'post\' or \'page\')
        \'normal\', // The part of the page where the metabox should be shown (\'normal\', \'advanced\', or \'side\')
        \'high\' // The priority within the context where the boxes should show (\'high\', \'core\', \'default\' or \'low\')
    );
}
add_action( \'add_meta_boxes\', \'create_custom_metabox\' );
 在这段代码之后,您将看到一个新的元框,但它将是空的,没有要显示的字段。
接下来,我们用新字段创建一个新的回调函数:(请参见中的函数名和回调参数add_meta_box() 上文)
function my_fields_callback( $post ) {
    // creating a custom nonce
    wp_nonce_field( basename( __FILE__ ), \'my_custom_nonce\' );
    // see and get if some meta is already saved
    $stored_meta = get_post_meta( $post->ID );
    ?>
    <!-- Textfield START -->
    <p>
        <span class="my-row-title">
            <label for="meta-text" class="my-row-title"><?php _e( \'Text\', \'textdomain\' )?></label>
        </span>
        <div class="my-row-content">
            <input type="text" name="meta-text" id="meta-text" placeholder="Text..." value="<?php if ( isset ( $stored_meta[\'meta-text\'] ) ) echo $stored_meta[\'meta-text\'][0]; ?>" />
        </div>
    </p>
    <!-- Textfield END -->
<?php
}
 在这段代码之后,您将看到一个新的输入字段和标签。但如果您输入了某些内容,它仍然不会被保存!因此,我们需要在此添加一个保存函数:
function save_my_meta( $post_id ) {
    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ \'my_custom_nonce\' ] ) && wp_verify_nonce( $_POST[ \'my_custom_nonce\' ], basename( __FILE__ ) ) ) ? \'true\' : \'false\';
    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }
    // save our new created field
    if( isset( $_POST[ \'meta-text\' ] ) ) {
        // if there is some content in the field, we update it
        update_post_meta( $post_id, \'meta-text\', sanitize_text_field( $_POST[ \'meta-text\' ] ) );
    }
}
add_action( \'save_post\', \'save_my_meta\' );
 之后,我们可以保存新字段的值。
如果要显示这些值,可以使用get_post_meta() 作用(Codex) 像这样:
$my_meta_value = get_post_meta( get_the_ID(), \'meta-text\', true );
if( !empty( $my_meta_value ) ) {
    echo $my_meta_value;
}
 因此,您可以看到需要几个函数。有很多教程,
here is an old one which still functions.对不起,我现在找不到德语的。
还要确保在保存和检索字段时注意清理字段的不同值。
我希望这有帮助!
<小时>
Update:
创建元框时
add_meta_box() 函数,您可以使用上下文参数设置元框的显示位置。。。
add_meta_box( 
            \'my_meta\', 
            __( \'My Setting\', \'textdomain\' ), 
            \'my_fields_callback\', 
            \'quicklink\',
            \'normal\', // The part of the page where the metabox should be shown (\'normal\', \'advanced\', or \'side\')
            \'high\'
        );
 如果您选择
side, 该框将在右侧边栏上创建。
Regarding ACF and fields without the wrapper container:
似乎当您使用ACF创建一个字段而不使用meta box包装器/容器时,一些元素被隐藏了,并且添加了一些额外的样式来隐藏容器!
因此,我认为最好的方法是加载一些自定义css,或者只在包含自定义字段的帖子类型的编辑/新建页面上加载jQuery。
function add_scripts_and_styles()
{
    global $typenow;
    if( \'quicklink\' != $typenow )
        return;         
    echo "
<script type=\'text/javascript\'>
    jQuery(document).ready( function($) {
        $(\'#my_meta\').removeClass(\'postbox\');
        $(\'#my_meta h3\').hide();
    });     
</script>
<style type=\'text/css\'>
    /* CUSTOM RULES */
</style>
";
}
add_action( \'admin_head-post-new.php\', \'add_scripts_and_styles\' );
add_action( \'admin_head-post.php\', \'add_scripts_and_styles\' );
Source: This Stackexchange answer
<人力资源>
Nonces:我试着解释一下,但也许抄本会让你更清楚:
WP环境中的nonce是一种安全特性。它是数字和字母的随机和唯一组合,仅在有限的时间内有效,并且每个用户都有效。
在这种情况下,我们在表单中使用它来检查输入数据是否来自真正的用户。系统将在保存时检查nonce,以查看是否没有人/其他人试图保存数据。
或者正如法典所说:
nonce字段用于验证表单请求的内容是否来自当前站点,而不是其他地方。
你可以阅读here 和here 关于nonces。