tammyhart / Reusable-Custom-WordPress-Meta-Boxes

177 stars 97 forks source link

Using fields #45

Open rliriano opened 11 years ago

rliriano commented 11 years ago

Could you please add how to output the repeatable fields on a page.

Thanks

ke6et commented 11 years ago

I need help with this too.

panarican commented 11 years ago

I forked this repo fixed some bugs and added onto it on my bitbucket. Account I made some helper functions for outputting field data as well.

I'll post a link later on tonight. On Jul 14, 2013 1:41 PM, "keihead" notifications@github.com wrote:

I need help with this too.

— Reply to this email directly or view it on GitHubhttps://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes/issues/45#issuecomment-20940186 .

ke6et commented 11 years ago

@panarican Wonderful. Does it explain how to output Images and the rest of the field types as well? Thanks for your time.

ke6et commented 11 years ago

I've managed to get the image data: <?php $sample_image = get_post_meta($post->ID, 'sample_image', true); echo wp_get_attachment_image($sample_image, 'thumbnail'); ?>

Still need help with the other field types though...will be checking forum later today for link :)

panarican commented 11 years ago

Here's my repo that contains bug fixes and template helper functions https://github.com/panarican/wordpress-boilerplate

Here's a link to the template helper functions: https://github.com/panarican/wordpress-boilerplate/blob/master/inc/helper/template.php

Repeatable Example (I hope this helps):

// use this inside your loop it will give u a preview of how all your meta data looks inside your post
pre_meta();

// let's say you have a field name of photos_images that's repeatable
// and you want to get the the field image "img" inside of the repeatable group

$photosImages = get('photos_images');

// make a foreach
foreach($photosImages as $item) {
   // $item would be the individual item inside of the $photosImages array
   // to get the field with the name "img" associated with it
   $img = $item['img'];

   // to output the repeated img you can do something like this
   // the first parameter is the id of the image attachement
   // the second parameter is an array of the size of the image
   echo img( $img , array(107,62) );
}
ke6et commented 11 years ago

@panarican hmm...with my poor skill in any of this, I could not understand anything within the files you linked to.

I just wanted to figure out how to display the data from fields on a page template, that's all. Thank you for your time and help!

panarican commented 11 years ago

OK you can do the following:

You can copy all the functions on template.php file I linked too then paste it into your functions.php file.

The snippet should still work just by doing that. If you have any questions about the example code I posted I can go into more detail. On Jul 14, 2013 7:42 PM, "keihead" notifications@github.com wrote:

hmm...with my poor skill in any of this, I could not understand anything within the files you linked to.

I just wanted to figure out how to display the data from fields on a page template, that's all. Thank you for your time and help!

— Reply to this email directly or view it on GitHubhttps://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes/issues/45#issuecomment-20946921 .

rliriano commented 11 years ago

@panarican thanks for the feedback, I was able to out put all the info like so:

$prefix = 'MA_';

$music_options = array( array( // Repeatable & Sortable Text inputs 'label' => 'Track Listing', //

function get_your_track_meta($array_values) {
    global $post;
        foreach ($array_values as &$value) {
            $html .= '<ul class="track-row">';
                     while (list($key, $field) = each($value)) {
                               $html .= '<li class="track-'.$key.'">'.$field.'</li>';
            }
            $html .= '</ul>';
        }
    return $html;
}
print get_your_track_meta($repeatable_fields);

It outputs each track as a ul list, hope this helps.

ke6et commented 11 years ago

@rliriano I'm still not comprehending :/ If you have spare time do you mind giving me a few more details?

Do you place that whole function inside of your page template? Or within functions.php?

Please help..

panarican commented 11 years ago

@keihead can u post your array you used to generate your custom meta box? That info would help me alot On Jul 16, 2013 3:05 PM, "keihead" notifications@github.com wrote:

@rliriano https://github.com/rliriano I'm still not comprehending :/ If you have spare time do you find giving me a few more details?

Do you place that whole function inside of your page template? Or within functions.php?

Please help..

— Reply to this email directly or view it on GitHubhttps://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes/issues/45#issuecomment-21065077 .

ke6et commented 11 years ago

@panarican No problem :)

$prefix = 'album_';
$fields = array(
    array( // Repeatable & Sortable Text inputs
        'label' => 'Repeatable', // 
panarican commented 11 years ago

Inside your while loop do this.

<?php
    $tracks = get_post_meta($post->ID, 'album_repeatable', true);
    foreach($tracks as $track) { ?>
        <div class="track">
            <h2><?php echo $track['title']; ?></h2>
            <div class="featuring"><?php echo $track['featuring']; ?></div>
            <div class="producer"><?php echo $track['producer']; ?></div>
            <div class="mp3"><a href="<?php echo $track['mp3']; ?>" target="_blank">Download MP3</a></div>
        </div>
    <?php 
 } ?>
ke6et commented 11 years ago

@panarican THANK YOU VERY MUCH :D Works wonderfully!!! I have one last question if you have time...

For other fields like Select Menu, Slider, Checkboxes etc. do I display the data with a similar code as you provided above?

panarican commented 11 years ago

If the field isn't an array of items (like a repeatable field). Then it would most likely be.

<?php $field = get_post_meta($post->ID, 'field_name', true); ?>
<div class="field"><?php echo $field; ?></div>
panarican commented 11 years ago

You're Welcome :+1: