• Resolved csjWP

    (@csjwp)


    CMB2/wiki/Field-Types#group

    I can’t figure out what is wrong with this code, why it isn’t showing up on the page. I’ve been follow this documentation on the wiki.

    functions.php

    
    /**
     * Front page carousel metabox
     */
    
    add_action( 'cmb2_admin_init', 'front_page_carousel' );
    function front_page_carousel() {
    
        $prefix = '_fp_';
    
        $cmb = new_cmb2_box( array(
            'id'            => $prefix . 'carousel_metabox',
            'title'         => __( 'Front-page carousel', 'cmb2' ),
            'object_types'  => array( 'page' ), // Post type
    	'show_on'      	=> array( 'key' => 'id', 'value' => array( 18 ) ),
            'context'       => 'normal',
            'priority'      => 'high',
            'show_names'    => true, // Show field names on the left
        ) );
    
        $group_field_id = $cmb->add_field( array(
            'id'            => $prefix . 'carousel_repeat_group',
    	'type'          => 'group',
    	'options'       => array(
                'group_title'   => __( 'Slide {#}', 'cmb2' ),
                'add_button'    => __( 'Add slide', 'cmb2' ),
                'remove_button' => __( 'Remove slide', 'cmb2' ),
                'sortable'      => true, // beta
            ),
        ) );
    
        $cmb->add_group_field( $group_field_id, array(
            'name' => 'Titel',
            'id'   => 'title',
            'type' => 'text',
        ) );
    
        $cmb->add_group_field( $group_field_id, array(
    	'name' => 'Image',
    	 'id'   => 'image',
    	'type' => 'file',
        ) );
    
        $cmb->add_group_field( $group_field_id, array(
    	'name' => __( 'Link', 'cmb2' ),
    	'id'   => 'link',
    	'type' => 'text_url',
    	'protocols' => array( 'http' ), // Array of allowed protocols
        ) );
    
    }
    

    content-front-page.php

    
    <div class="carousel">
        <?php
     
    	$entries = get_post_meta( get_the_ID(), $prefix . 'carousel_metabox', true );
    
    	foreach ( (array) $entries as $key => $entry ) {
    
    		$img = $title = $link = '';
    
    		if ( isset( $entry['image_id'] ) ) {
    			$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
    				'class' => 'full',
    			) );
    		}
    
    		if ( isset( $entry['title'] ) ) {
    			$title = esc_html( $entry['title'] );
    		}
    
    		if ( isset( $entry['link'] ) ) {
    			$link = esc_html( $entry['link'] );
    		}
    
    		echo $entries;
    	}
        ?>
    </div><!-- .carousel -->
    

    All I get is an black page with an empty carousel div and tears in my eyes

    • This topic was modified 8 years, 6 months ago by csjWP.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Your get_post_meta() call is using the wrong meta key. You are using the metabox ID when it should be the group field id, '_fp_carousel_repeat_group'

    Thread Starter csjWP

    (@csjwp)

    Hi Justin,

    Was actually the first key i tried, since that is the way it’s done in the documentation. But it’s the same. I’m still not getting anything.

    1. First I did this,
    $entries = get_post_meta( get_the_ID(), '_fp_carousel_repeat_group', true );

    2. Then added the $prefix like this,
    $entries = get_post_meta( get_the_ID(), $prefix . 'carousel_repeat_group', true );

    3. And then finally I switched,
    $entries = get_post_meta( get_the_ID(), $prefix . 'carousel_metabox', true

    But I’m gonna leave it as 2 without the §prefix . going forward and see if I can get it working.

    Thread Starter csjWP

    (@csjwp)

    Sorry but I forgot some details.

    I added var_dump($entries); right before echo $entries;

    And it spits out this. So it is working, I just don’t seem to be able to echo it out.

    array(1) {
      [0]=>
      array(4) {
        ["title"]=>
        string(10) "test title"
        ["image_id"]=>
        int(112)
        ["image"]=>
        string(75) "https://example..com/wp-content/uploads/2016/09/image.jpg"
        ["link"]=>
        string(19) "https://example..com"
      }
    }
    • This reply was modified 8 years, 6 months ago by csjWP.
    • This reply was modified 8 years, 6 months ago by csjWP.
    Thread Starter csjWP

    (@csjwp)

    I’m gonna shoot myself and die from shame ??

    
    echo $img;
    echo $title;
    echo $link;
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘CMB2 repeatable group not showing up on page’ is closed to new replies.