WordPress插件自定义字段不显示表单

时间:2017-11-15 17:23:27

标签: php wordpress custom-post-type custom-fields

好的,所以我找不到一个可以做我想要的插件,所以我开始创建自己的插件。我以前做过PHP编码,虽然已经有一段时间了。抓住一堆互联网代码来修改和做我想要的东西,把它们放在一个单独的课堂上。

目前的目标是拥有一个包含自定义字段的新帖子类型,并且能够在管理网站中编辑这些字段。我所拥有的是一种新的帖子类型,带有一堆带有正确标签的盒子,但没有显示任何形式。

这可能只是需要第二眼,但我不明白这里有什么问题。 add_meta_box调用似乎正在工作,它们只是不回显回调函数中的任何内容。我错过了什么?非常感谢任何帮助。

如果需要,整个回购是https://github.com/steev42/WrestleEFedManager;相关文件复制如下。

<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'Wrestleefedmanager_Federation' ) ) :
class Wrestleefedmanager_Federation {

    /**
     * Constructor
     */
    public function __construct() {
        // Hooking up our function to theme setup
        add_action( 'init',             array( $this, 'create_federation_post_type' ) ); 
        add_action( 'add_meta_boxes',   array( $this, 'initialize_federation_post_type') );
        add_action( 'save_post',        array( $this, 'save_fed') );
    }

    // Our custom post type function
    function create_federation_post_type() {

     $fedlabels = array(
        'name'                => _x( 'Federations', 'Post Type General Name'),
        'singular_name'       => _x( 'Federation', 'Post Type Singular Name'),
        'menu_name'           => __( 'Federations'),
        'parent_item_colon'   => __( 'Parent Fed'),
        'all_items'           => __( 'All Feds'),
        'view_item'           => __( 'View Fed'),
        'add_new_item'        => __( 'Add New Fed'),
        'add_new'             => __( 'Add New'),
        'edit_item'           => __( 'Edit Fed'),
        'update_item'         => __( 'Update Fed'),
        'search_items'        => __( 'Search Feds'),
        'not_found'           => __( 'Not Found'),
        'not_found_in_trash'  => __( 'Not found in Trash'),
    );

    $fedargs = array(
        'label'               => __( 'feds' ),
        'description'         => __( 'Federations' ),
        'labels'              => $fedlabels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'author', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        //'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

        register_post_type( 'feds', $fedargs);
    }


    function initialize_federation_post_type() {
        /*
        add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, 
                      string $context = 'advanced', string $priority = 'default', array $callback_args = null )*/
        add_meta_box("abbreviation", "Abbreviation", "fed_abbr", "feds", "normal", "low");
        add_meta_box("founded", "Founded", "fed_founded", "feds", "side", "low");
        add_meta_box("closed", "Closed", "fed_closed", "feds", "side", "low");
        add_meta_box("parentfed", "Parent Federation", "fed_parent", "feds", "normal", "low");
        add_meta_box("logo", "Logo", "fed_logo", "feds", "normal", "low");
        add_meta_box("owner", "Owner", "fed_owner", "feds", "side", "low");     
    }

    function fed_abbr(){
        global $post;
        $custom = get_post_custom($post->ID);
        $abbr = $custom["abbreviation"][0];
        ?>
        <label>Abbr:</label>
        <input name="fed_abbreviation" type="text" value="<?php echo $abbr; ?>" />
        <?php
        }

    function fed_founded(){
        global $post;
        $custom = get_post_custom($post->ID);
        $founded = $custom["founded"][0];
        ?>
        <label>Founded:</label>
        <input name="fed_founddate" value="<?php echo $founded; ?>" />
        <?php
    }

    function fed_closed(){
        global $post;
        $custom = get_post_custom($post->ID);
        $closed = $custom["closed"][0];
        ?>
        <label>Closed:</label>
        <input name="fed_closedate" value="<?php echo $closed; ?>" />
        <?php
    }

    function fed_parent(){
    }

    function fed_logo(){
    }

    function fed_owner(){
        global $post;
        $custom = get_post_custom($post->ID);
        $owner = $custom["owner"][0];
        ?>
        <label>Owner:</label>
        <input name="fed_owner" value="<?php echo $owner; ?>" />
        <?php
    }

    function save_fed(){
        global $post;

        update_post_meta($post->ID, "abbreviation", $_POST["fed_abbreviation"]);
        update_post_meta($post->ID, "founded", $_POST["fed_founddate"]);
        update_post_meta($post->ID, "closed", $_POST["fed_closedate"]);
        update_post_meta($post->ID, "owner", $_POST["fed_owner"]);
    }


}
endif;

1 个答案:

答案 0 :(得分:0)

好的,所以通过一些试验和错误并搜索谷歌,我能够做到这一点。

首先,因为在类中,add_meta_box中的回调需要class User extends Model { public function units(){ return $this->hasMany(Unit::class, 'owner'); } } 格式而不仅仅是原始字符串。

其次,array ($this, <cb>)的参数不是使用add_meta_boxes动作回调,而是有自己的选项,我就这样使用了它:register_post_type

因此,在完成该工作并添加一些代码以使字段以视图模式显示之后,该文件如下所示:

'register_meta_box_cb' => array( $this, 'initialize_federation_post_type')