1

I am trying to create a custom meta box that lets you add rows dynamically. Following code snippet works fine and and saves data in my edit page section. However, I can't get it to display the data on the actual page:

    add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

    /* Do something with the data entered */
    add_action( 'save_post', 'dynamic_save_postdata' );

    /* Adds a box to the main column on the Post and Page edit screens */
    function dynamic_add_custom_box() {
        add_meta_box(
            'dynamic_sectionid',
    __( 'My Tracks', 'myplugin_textdomain' ),
    'dynamic_inner_custom_box',
    'page');
    }

    /* Prints the box content */
     function dynamic_inner_custom_box() {
        global $post;
        // Use nonce for verification
        wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
?>
<div id="meta_inner">
<?php

//get the saved meta as an arry
$songs = get_post_meta($post->ID,'songs',true);

$c = 0;
if ( count( $songs ) > 0 ) {
    foreach( $songs as $track ) {
        if ( isset( $track['title'] ) || isset( $track['track'] ) ) {
            printf( '<p>Song Title <input type="text" name="songs[%1$s][title]" value="%2$s" /> -- Track number : <input type="text" name="songs[%1$s][track]" value="%3$s" /><span class="remove">%4$s</span></p>', $c, $track['title'], $track['track'], __( 'Remove Track' ) );
            $c = $c +1;
        }
    }
}

        ?>
    <span id="here"></span>
    <span class="add"><?php _e('Add Tracks'); ?></span>
    <script>
        var $ =jQuery.noConflict();
        $(document).ready(function() {
            var count = <?php echo $c; ?>;
            $(".add").click(function() {
                count = count + 1;

        $('#here').append('<p> Song Title <input type="text" name="songs['+count+'][title]" value="" /> -- Track number : <input type="text" name="songs['+count+'][track]" value="" /><span class="remove">Remove Track</span></p>' );
        return false;
    });
    $(".remove").live('click', function() {
        $(this).parent().remove();
    });
});
</script>
    </div><?php

    }

    /* When the post is saved, saves our custom data */
    function dynamic_save_postdata( $post_id ) {
        // verify if this is an auto save routine. 
        // If it is our form has not been submitted, so we dont want to do anything
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;

        // verify this came from the our screen and with proper authorization,
        // because save_post can be triggered at other times
        if ( !isset( $_POST['dynamicMeta_noncename'] ) )
            return;

        if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
            return;

        // OK, we're authenticated: we need to find and save the data

        $songs = $_POST['songs'];

        update_post_meta($post_id,'songs',$songs);
    }

As you can see, 'page' is referring to page where I want to post to. Now when I loop for the results on the page template nothing shows up.

     <?php            
        $args = array(
'post_type' => 'page',   
    );  
    $repeat_loop = new WP_Query( $args ); if ( $repeat_loop->have_posts() ) : while ( $repeat_loop->have_posts() ) : $repeat_loop->the_post();
    $meta = get_post_meta( $post->ID, 'songs', true ); ?>                  
    <?php echo $meta['title']; ?><?php echo $meta['track']; ?>
         <?php endwhile; 
             endif;
                wp_reset_postdata(); 
            ?>

I do get results when I use the 'var_dump($posts);'. I just can't get it to display in the page.

1

Try changing $meta['title'] to $meta['title'][0] & the same thing for $meta['track'] . change it to $meta['track'][0]

UPDATE:

I looked at the code more deeply and you basically need a foreach loop. Here's the final code should look like:

<?php
$args = array(
    'post_type' => 'page'
);  
$repeat_loop = new WP_Query( $args ); if ( $repeat_loop->have_posts() ) : while ( $repeat_loop->have_posts() ) : $repeat_loop->the_post();
$meta = get_post_meta( $post->ID, 'songs', true ); ?>                  

<?php
foreach ($meta as $key => $value) {
    echo $value['title']; ?><?php echo $value['track'];
}
?>
<?php endwhile; 
    endif;
    wp_reset_postdata(); 
?>

Thanks

3
  • Thanks! Your approach is a hint in the right direction. I tried it and when I use $meta[0]['title'] (move the order around) I get the first result. But since it is an array I want to loop through all results and display all of them. Do you have any ideas? Mar 8 '17 at 16:21
  • Answer edited. This will work for you. Mar 8 '17 at 16:26
  • Haha, looks like our answers have been overlapping. Thank you very much for your help! Mar 8 '17 at 16:39
0

Ok, I solved it thanks to the hint from Abdul! I need to loop through my array so I implemented a simple for loop:

    <?php for( $i= 0 ; $i <= 10 ; $i++ ) echo '<p>' .$meta[$i]['title']. '</p>';?>

Thanks a lot for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.