我已经在wordpress中创建了自定义帖子类型插件,但在添加新内容时未显示元框?

时间:2018-12-06 09:18:07

标签: php wordpress

我已经创建了名为DailyCostPlugin的插件,并启动register_post_type函数来创建名为Daily Cost的自定义帖子。我想通过add_action add_action( 'admin_init',array( $this, create_meta_cost_box' ));添加名为Daily Cost Details的元框,它将其放入函数{{1 }},但未显示名为“每日成本明细”的元框,也没有转到create_meta_cost_box中调用的名为display_cost_box的函数,这是代码

add_meta_box.

1 个答案:

答案 0 :(得分:0)

在这里,让我们像这样修改代码

<?php
    /*
    Plugin Name: Daily Cost Plugin
    Plugin URI: http://biney.com/plugin
    Description: This is my First attempt
    Version: 1.0.0
    Author: Biney
    Author URI: http://biney.com
    License: GPLv2 or later
    Text Domain: daily-cost-plugin
    */

    defined( 'ABSPATH' ) or die("Hey, you can\t access!");
    class DailyCostPlugin{

        function __construct(){
            add_action( 'init',array( $this,'daily_cost_custom_post' ));
            add_action( 'admin_init',array( $this, 'create_meta_cost_box' ));
        }

        function daily_cost_custom_post(){
            register_post_type('daily_costs', [
                    'public' => true,
                    'menu_position' => 15,
                    'supports'      => array( 'title'),
                    'taxonomies'    => array( '' ),
                    'menu_icon'     => plugins_url( 'images/image.png', __FILE__ ),
                    'has_archive'   => true,
                    'labels' => array(
                        'name' => 'Daily Costs',
                        'singular_name'         => 'Daily Cost',
                        'add_new'               => 'Add New',
                        'add_new_item'          => 'Add New Daily Cost',
                        'edit'                  => 'Edit',
                        'edit_item'             => 'Edit Daily Cost',
                        'new_item'              => 'New Daily Cost',
                        'view'                  => 'View',
                        'view_item'             => 'View Daily Cost',
                        'search_items'          => 'Search Daily Costs',
                        'not_found'             => 'No Daily Costs found',
                        'not_found_in_trash'    => 'No Daily Costs found in Trash',
                        'parent'                => 'Parent Daily Cost'
                    )
                ]
            );
        }
        function create_meta_cost_box(){
            //didn't show meta bow having title Daily Cost Details
            add_meta_box( 'daily_cost_meta_box',
                'Daily Cost Details',
                array( $this,'display_cost_box' ),
                'daily_costs', 'normal', 'high'
            );
        }

        function display_cost_box( $daily_cost ) {

            // Retrieve current cost and rating based on daily cost ID
            $daily_cost_value = esc_html( get_post_meta( $daily_cost->ID,
                'daily_cost_value', true ) );
            $daily_cost_rating = intval( get_post_meta( $daily_cost->ID,
                'daily_cost_rating', true ) );
            ?>
            <table>
                <tr>
                    <td style="width: 100%">Daily Cost</td>
                    <td><input type="text" size="80" name="daily_cost_value" value="
         <?php echo $daily_cost_value; ?>" /></td>
                </tr>
                <tr>
                    <td style="width: 150px">Daily Cost Rating</td>
                    <td>
                        <select style="width: 100px" name="daily_cost_rating">
                            <?php
                            // Generate all items of drop-down list
                            for ( $rating = 5; $rating >= 1; $rating -- ) {
                            ?>
                            <option value="<?php echo $rating; ?>" <?php echo
                            selected( $rating, $daily_cost_rating ); ?>>
                                <?php echo $rating; ?> stars <?php } ?>
                        </select>
                    </td>
                </tr>
            </table>
            <?php
        }
        function activate(){
            //generate a cpt
            //flush rewrite rules
            flush_rewrite_rules();
        }
        function deactivate(){
            //flush rewrite rules
            flush_rewrite_rules();
        }
        function uninstall(){
            //delete cpt
            //delte all the plugin data from the DB
        }
    }
    if( class_exists( 'DailyCostPlugin' ) ){
        $dailyCostPlugin = new DailyCostPlugin();
    }
    //activation
    register_activation_hook( __FILE__, array( $dailyCostPlugin, 'activate'));
    //deactivation
    register_deactivation_hook( __FILE__, array( $dailyCostPlugin,'deactivate'));