Adding custom image fields and other fields at the same time

前端 未结 3 1661
粉色の甜心
粉色の甜心 2020-12-01 17:55

I basically want to have a custom CMS page that has pairs of images and labels for those images defined within it. I intend to use these pairs of items for populating conte

3条回答
  •  时光说笑
    2020-12-01 18:25

    I used @brasofilo's answer above, however I really missed having the image ID saved as well, so I could use wp_get_attachment_imagefor example.

    So here's my revised code, which also saves the image ID. I also use two arrays to define on which page templates and respectively on which post types the meta box appears. Here's the code:

    ID, '_wp_page_template', true ), $rep_fields_templates ) ) {
            add_meta_box(
                'post_gallery',
                'Slideshow Gallery',
                'post_gallery_options_so_14445904',
                'page',
                'normal',
                'core'
            );
        }
    }
    
    /**
     * Print the Meta Box content
     */
    function post_gallery_options_so_14445904() 
    {
        global $post;
        $gallery_data = get_post_meta( $post->ID, 'gallery_data', true );
    
        // Use nonce for verification
        wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_so_14445904' );
        ?>
    
        
       
    ID ) ) { ?>

    Make sure the number if images you add is a multiple of 5.

    ID, '_wp_page_template', true ), $rep_fields_templates ) && !in_array( get_post_type( $post->ID) , $rep_fields_posts ) ) return; ?> post_type ) return; // Verify authenticity if ( !wp_verify_nonce( $_POST['noncename_so_14445904'], plugin_basename( __FILE__ ) ) ) return; global $rep_fields_templates, $rep_fields_posts; if ( !in_array( get_post_meta( $post_id, '_wp_page_template', true ), $rep_fields_templates ) && !in_array( get_post_type( $post_id) , $rep_fields_posts ) ) return; if ( $_POST['gallery'] ) { // Build array for saving post meta $gallery_data = array(); for ($i = 0; $i < count( $_POST['gallery']['image_url'] ); $i++ ) { if ( '' != $_POST['gallery']['image_url'][ $i ] ) { $gallery_data['image_url'][] = $_POST['gallery']['image_url'][ $i ]; $gallery_data['image_id'][] = $_POST['gallery']['image_id'][ $i ]; $gallery_data['image_desc'][] = $_POST['gallery']['image_desc'][ $i ]; } } if ( $gallery_data ) update_post_meta( $post_id, 'gallery_data', $gallery_data ); else delete_post_meta( $post_id, 'gallery_data' ); } // Nothing received, all fields are empty, delete option else { delete_post_meta( $post_id, 'gallery_data' ); } }

    Here's an example of how you can output the data to your theme.

    
    

提交回复
热议问题