首页 > 解决方案 > 帖子编辑器中未显示 Wordpress 元字段

问题描述

使用 WP 和自定义元字段有点新。

我正在尝试学习 REST API 并创建了一个插件来注册自定义元字段。这是插件:

/**
 * @package api_testing
 * @version 1.0.0
 */
/*
Plugin Name: API Testing
Plugin URI: http://example.com/
Description: The beginning of an awesome plugin
Author: Me
Version: 1.0.0
Author URI: http://example.com/
*/

function generate_press_type() {
    $labels = array(
        'name'                  => 'Press Articles',
        'singular_name'         => 'Press',
    );
    $args = array(
        'label'                 => 'Press',
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'custom-fields', 'thumbnail' ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'capability_type'       => 'post',
        'show_in_rest'          => true,
        'rest_base'             => 'press-articles',
        'rewrite'               => true,
        'rewrite_slug'          => '',        
    );
    register_post_type( 'press_article', $args );

    $meta_args = array(
        'type'         => 'string',
        'description'  => 'source',
        'single'       => true,
        'show_in_rest' => true,
    );

    register_post_meta( 'press_article', 'source', $meta_args );
    
}
add_action( 'init', 'generate_press_type', 0 );

我可以在 WP 后端看到类型“press”并创建帖子。当我查询它们时,元字段会显示在有效负载中。我只是想知道是否有可能让该字段显示在帖子编辑器中,我看到了自定义字段列表,但我的不包括在内:

在此处输入图像描述

想知道我在注册帖子类型或元数据时是否错过了一个 arg?我查看了文档,似乎我已经拥有了所有内容,但不确定...

标签: phpwordpresscustom-fieldswordpress-rest-api

解决方案


您必须添加自定义元框才能显示该字段。请在此处查看 - https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/


推荐阅读