Last active
April 23, 2020 18:29
-
-
Save wpjess/d46b3b46eea86eac9e796d1a9cd7425c to your computer and use it in GitHub Desktop.
WP custom meta boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$slides = new_cmb2_box(array( | |
'id' => 'slides_metabox', | |
'title' => 'Slides', | |
'object_types' => array( | |
'slides', | |
) , // Post type | |
'context' => 'normal', | |
'priority' => 'high', | |
'show_names' => true, | |
)); | |
$slides->add_field(array( | |
'name' => __('Link URL', 'cmb2') , | |
'desc' => __('optional', 'cmb2') , | |
'id' => $prefix . 'slide_link', | |
'type' => 'text', | |
// 'repeatable' => true, | |
)); | |
$slides->add_field(array( | |
'name' => __('Slide Image', 'cmb2') , | |
'desc' => __('Upload an image or enter a URL.', 'cmb2') , | |
'id' => $prefix . 'slide_image', | |
'type' => 'file', | |
)); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////// | |
/////////////// FETCH THE CUSTOM META BOXES ///////////////// | |
////////////////////////////////////////////////////////////// | |
if ( file_exists( __DIR__ . '/post-types/metabox/init.php' ) ) { | |
require_once __DIR__ . '/post-types/metabox/init.php'; | |
} | |
add_action( 'cmb2_admin_init', 'jess_metaboxes' ); | |
function jess_metaboxes() { | |
$prefix = ''; | |
require_once(TEMPLATEPATH . '/post-types/metabox/page-meta.php'); | |
require_once(TEMPLATEPATH . '/post-types/metabox/slide-meta.php'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$slides = get_post_meta($post->ID, 'slides', true); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $slides = get_post_meta($post->ID, 'slides', true); ?> | |
<?php foreach ( $slides as $slide ) { ?> | |
<?php | |
$image = $slide['slide_image']; | |
?> | |
<img src="<?php echo $slide['slide_image']; ?>" alt="" /> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment