【问题标题】:Adding multiple meta-boxes to a custom post type in wordpress在 wordpress 中将多个元框添加到自定义帖子类型
【发布时间】:2012-12-03 22:06:46
【问题描述】:

我有以下代码将自定义帖子类型添加到管理破折号和自定义元框到帖子编辑窗口:

function teasers_custom_init() {
  $labels = array(
    'name' => 'Teasers',
    'singular_name' => 'Teaser',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Teasers',
    'edit_item' => 'Edit Teaser',
    'new_item' => 'New Teaser',
    'all_items' => 'All Teasers',
    'view_item' => 'View Teaser',
    'search_items' => 'Search Teasers',
    'not_found' =>  'No teasers found',
    'not_found_in_trash' => 'No teasers found in Trash', 
    'parent_item_colon' => 'Parent Page',
    'menu_name' => 'Teasers'
  );

  $args = array(
    'labels' => $labels,
    'description' => 'set slider panels with loop times',
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'Teasers' ),
    'capability_type' => 'page',
    'has_archive' => true, 
    'hierarchical' => true,
    'menu_position' => 60,
    'supports' => array( 'title', 'thumbnail', 'page-attributes'), 

  ); 

  register_post_type( 'teasers', $args );
}
add_action( 'init', 'teasers_custom_init' );


//adding the meta box when the admin panel initialises
add_action("admin_init", "admin_init");
// this adds the save teaser function on save post
add_action('save_post', 'save_teaser');

function admin_init(){
    add_meta_box('teaser_loop', 'Loop Time', 'loop_meta', 'teasers', 'normal', 'default');
}
// callback function of add meta box that displays the meta box in the post edit screen
function loop_meta($post, $args){

    $teaser_loop = get_post_meta($post->ID, 'teaser_loop', true);

?>
    <label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/>

<?php

}

// saving the teaser
function save_teaser(){
    global $post;
    update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']);  
}

我的问题是,如果我想添加一个额外的元框,最好的方法是什么?

我尝试在 admin_init 函数中添加另一个 add_meta_box 调用,并为此元框 html 创建了一个额外的回调函数,但前端没有生成任何内容。任何指针都会很棒。

编辑:这就是我为多个元框做的方式(这有效):

//adding the meta box when the admin panel initialises
    add_action("admin_init", "admin_init");
    // this adds the save teaser function on save post
    add_action('save_post', 'save_teaser');
    function admin_init(){
        add_meta_box('teaser_loop', 'Loop Time', 'loop_meta_1', 'teasers', 'normal', 'default');
        add_meta_box('teaser_link', 'Teaser Link', 'loop_meta_2', 'teasers', 'normal', 'default');
    }
    // back function of add meta box that displays the meta box in the post edit screen
    function loop_meta_1($post, $args){


        $teaser_loop = get_post_meta($post->ID, 'teaser_loop', true);

?>
    <label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/>

<?php

}

    function loop_meta_2($post, $args){

        $teaser_link = get_post_meta($post->ID, 'teaser_link', true);

?>
    <label>Teaser Link: </label><input type="text" name="teaser_link" value="<?php echo $teaser_link; ?>" /><br/>

<?php

}

// saving the teaser
function save_teaser(){
    global $post;
    update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']);
    update_post_meta($post->ID, 'teaser_link', $_POST['teaser_link']);
}

【问题讨论】:

  • 你也可以在wordpress.stackexchange.com问这个
  • Code Review,因为这是旨在优化的工作代码。
  • 我提供了赏金,因为网络上没有任何地方可以动态解决这个问题。我想知道包含所有表单元素的最佳解决方案是什么。
  • 太棒了,这正是我想要的。谢谢!

标签: wordpress custom-post-type


【解决方案1】:

这是添加 2 个元框的非常不恰当的方法吗?它可以工作,但我认为它可能是低效的代码/php。原文发在https://typerocket.com

// Add form and basic text field
function press_meta_box(WP_Post $post) {
    add_meta_box('press_meta', 'Press Release Date', function() use ($post) {
        $field_name = 'press_date';
        $field_name2 = 'press_link';
        $field_value = get_post_meta($post->ID, $field_name, true);
        $field_value2 = get_post_meta($post->ID, $field_name2, true);
        wp_nonce_field('study_nonce', 'study_nonce');
        ?>
        <table class="form-table">
            <tr>
                <th> <label for="<?php echo $field_name; ?>">Press Release Date (MM/DD/YYYY)</label></th>
                <td>
                    <input id="<?php echo $field_name; ?>"
                           name="<?php echo $field_name; ?>"
                           type="text"
                           value="<?php echo esc_attr($field_value); ?>" />
                </td>
            </tr>
            <tr>
                <th> <label for="<?php echo $field_name2; ?>">Full link to pdf</label></th>
                <td>
                    <input id="<?php echo $field_name2; ?>"
                           name="<?php echo $field_name2; ?>"
                           type="text"
                           value="<?php echo esc_attr($field_value2); ?>" />
                </td>
            </tr>
        </table>
        <?php
    });
}
// Check for empty string allowing for a value of `0`
function empty_str( $str ) {
    return ! isset( $str ) || $str === "";
}
// Save and delete meta but not when restoring a revision
add_action('save_post', function($post_id){
    $post = get_post($post_id);
    $is_revision = wp_is_post_revision($post_id);
    $field_name = 'press_date';
    $field_name2 = 'press_link';

    // Do not save meta for a revision or on autosave
    if ( $post->post_type != 'press_release' || $is_revision )
        return;

    // Do not save meta if fields are not present,
    // like during a restore.
    if( !isset($_POST[$field_name]) )
        return;

    // Secure with nonce field check
    if( ! check_admin_referer('study_nonce', 'study_nonce') )
        return;

    // Clean up data
    $field_value = trim($_POST[$field_name]);
$field_value2 = trim($_POST[$field_name2]);
    // Do the saving and deleting
    if( ! empty_str( $field_value ) ) {
        update_post_meta($post_id, $field_name, $field_value);
    } elseif( empty_str( $field_value ) ) {
        delete_post_meta($post_id, $field_name);
    }
     // Do the saving and deleting 2
    if( ! empty_str( $field_value2 ) ) {
        update_post_meta($post_id, $field_name2, $field_value2);
    } elseif( empty_str( $field_value2 ) ) {
        delete_post_meta($post_id, $field_name2);
    }
});

【讨论】:

    【解决方案2】:

    它可以完全封装在一个类中。在这里,我不处理添加自定义帖子类型,并且只有两个简单的输出字段,文本和复选框。一个完整的工作代码应该处理每个所需的输入类型。

    <?php
    /**
     * Plugin Name: Sample Dynamic Meta Boxes
     * Plugin URI:  http://stackoverflow.com/q/13903529/1287812
     * Author:      brasofilo
     */
    class B5F_Dynamic_Meta_Boxes 
    {
        private $boxes;
    
        # Safe to start up
        public function __construct( $args )
        {
            $this->boxes = $args;
            add_action( 'plugins_loaded', array( $this, 'start_up' ) );
        }
    
        public function start_up()
        {
            add_action( 'add_meta_boxes', array( $this, 'add_mb' ) );
        }
    
        public function add_mb()
        {
            foreach( $this->boxes as $box )
                add_meta_box( 
                    $box['id'], 
                    $box['title'], 
                    array( $this, 'mb_callback' ), 
                    $box['post_type'], 
                    isset( $box['context'] ) ? $box['context'] : 'normal', 
                    isset( $box['priority'] ) ? $box['priority'] : 'default', 
                    $box['args']
                );
        }
    
        # Callback function, uses helper function to print each meta box
        public function mb_callback( $post, $box )
        {
            switch( $box['args']['field'] )
            {
                case 'textfield':
                    $this->textfield( $box, $post->ID );
                break;
                case 'checkbox':
                    $this->checkbox( $box, $post->ID );
                break;
            }
        }
    
        private function textfield( $box, $post_id )
        {
            $post_meta = get_post_meta( $post_id, $box['id'], true );
            printf(
                '<label>%s: <input type="text" name="%s" value="%s" /></label> <small>%s</small><br/>',
                $box['title'],
                $box['id'],
                $post_meta,
                $box['args']['desc']
            );
        }
    
        private function checkbox( $box, $post_id )
        {
            $post_meta = get_post_meta( $post_id, $box['id'], true );
            printf(
                '<label>%s: </label><input type="checkbox" name="%s" %s /> <small>%s</small><br/>',
                $box['title'],
                $box['id'],
                checked( 1, $post_meta, false ),
                $box['args']['desc']
            );
        }
    }
    
    # ADD TWO META BOXES - DIFFERENT POST TYPES - DIFFERENT CONTEXTS AND PRIORITIES
    $args = array(
        array(
            'id' => 'teaser_loop',
            'title' => 'Loop Time',
            'post_type' => 'post',
            'args' => array(
                'desc' => 'Enter the time',
                'field' => 'textfield',
            )
        ),
        array(
            'id' => 'teaser_link',
            'title' => 'Loop Link',
            'post_type' => 'page',
            'context' => 'side',
            'priority' => 'high',
            'args' => array(
                'desc' => 'Open link',
                'field' => 'checkbox',
            )
        ),
    );
    new B5F_Dynamic_Meta_Boxes( $args );
    
    # ADD ANOTHER META BOX TO ANOTHER POST TYPE
    $more_args = array(
        array(
            'id' => 'extra_box',
            'title' => 'And another one',
            'post_type' => 'teaser',
            'args' => array(
                'desc' => 'Open link',
                'field' => 'textfield',
            )
        ),
    );
    new B5F_Dynamic_Meta_Boxes( $more_args );
    

    这只是一个骨架,从这里有很多要写的。一些例子:

    【讨论】:

    • 这仅限于文本输入。如果您使用单选框/复选框、下拉菜单或其他表单元素,这将不起作用。我喜欢第一部分,但似乎有点过度设计
    • @Joren,好吧,这只是一个大纲。使用类会更合适。微调它取决于用户。您没有在赏金中添加自定义描述,您可以在问题中发表评论说明您的期望吗?
    • @brasofilo:是否可以使用您的class 我可以在单个元框中添加多个元字段,并且也可以在那里添加选项卡功能?
    • @MayeenulIslam,您可以修改以下内容:pt.stackoverflow.com/a/10564/201
    【解决方案3】:

    您可以使用以下代码将元框添加到自定义帖子类型。

    首先,创建元框

    add_action('admin_init', 'my_theme_on_admin_init');
    
    function my_theme_on_admin_init() {
        add_meta_box('my_metabox',
            __('My metabox', 'textdomain'),
            'my_metabox_render',
            'my_custom_post_type', 'normal', 'high'
        );
    }
    

    注意,my_custom_post_type 是您自定义帖子类型的名称,my_metabox_render - 呈现元框的函数名称。

    渲染函数应该创建所有必要的字段

    function my_metabox_render($post) {
        $data = get_post_meta($post->ID, '_meta_key', true);
        // Use nonce for verification
        wp_nonce_field('add_my_meta', 'my_meta_nonce');
    ?>
    <div class="inside">
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="my_meta_value"><?php _e('My meta', 'textdomain'); ?></label></th>
                <td><textarea id="my_meta_value" name="my_meta_value" cols="40" rows="5"><?php echo (isset($data)) ? $data : ''; ?></textarea></td>
            </tr>
        </table>
    </div>
    <?php
    }
    

    你应该在用户保存帖子时更新元数据

    add_action('wp_insert_post', 'save_my_meta', 10, 2);
    
    function save_my_meta($id) {
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
            return $id;
        if (!current_user_can('edit_posts'))
            return;
    
        if (!isset($id))
            $id = (int) $_REQUEST['post_ID'];
    
        if (isset($_POST['my_meta_value']) && wp_verify_nonce($_POST['my_meta_value'], 'add_my_meta')) {
            $data = $_POST['my_meta_value'];
            if (isset($data)) {
                update_post_meta($id, '_meta_key', $data);
            }
            else {
                delete_post_meta($id, '_meta_key');
            }
        }
    }
    

    【讨论】:

    • 嗨弗拉基米尔。感谢您的回复。添加一个元框很好,但是如何将多个元框添加到自定义帖子类型?这就是我遇到的问题。
    • 为您要创建的每个元框调用 add_meta_box 函数。当然,每个元框都应该有自己的渲染功能。
    • 你能显示你试图添加几个元框的代码吗?
    • 添加了添加两个的代码,现在可以使用了。不确定这是否是最好的方法,但对我有用。
    猜你喜欢
    • 2011-11-19
    • 2013-08-04
    • 2014-08-29
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2021-05-22
    相关资源
    最近更新 更多