Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using fields #45

Open
rliriano opened this issue Jul 11, 2013 · 15 comments
Open

Using fields #45

rliriano opened this issue Jul 11, 2013 · 15 comments

Comments

@rliriano
Copy link

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

Thanks

@ke6et
Copy link

ke6et commented Jul 14, 2013

I need help with this too.

@panarican
Copy link

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" [email protected] wrote:

I need help with this too.


Reply to this email directly or view it on GitHubhttps://github.com//issues/45#issuecomment-20940186
.

@ke6et
Copy link

ke6et commented Jul 14, 2013

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

@ke6et
Copy link

ke6et commented Jul 14, 2013

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
Copy link

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
Copy link

ke6et commented Jul 14, 2013

@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
Copy link

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" [email protected] 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//issues/45#issuecomment-20946921
.

@rliriano
Copy link
Author

@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', //
'desc' => 'Track Listing, click on the green icon to add more tracks.', // description
'id' => $prefix.'tracks', // field id and name
'type' => 'repeatable', // type of field
'sanitizer' => array( // array of sanitizers with matching kets to next array
'number' => 'sanitize_text_field',
'title' => 'sanitize_title',
'lyric' => 'wp_kses_data'
),
'repeatable_fields' => array ( // array of fields to be repeated
'number' => array(
'label' => 'Track #',
'id' => 'number',
'width' => 'width:10%;',
'type' => 'text'
),
'title' => array(
'label' => 'Track Title',
'id' => 'title',
'width' => 'width:80%;',
'type' => 'text'
),
'lyric' => array(
'label' => 'Lyric',
'id' => 'lyric',
'rows' => '3',
'width' => 'width:80%;',
'desc' => 'Add track lyrics here (optional).',
'type' => 'textarea'
)
)
)
)
// Within my loop
global $repeatable_fields;
$repeatable_fields = get_post_meta($post->ID, 'MA_tracks', true);

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
Copy link

ke6et commented Jul 16, 2013

@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
Copy link

@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" [email protected] 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//issues/45#issuecomment-21065077
.

@ke6et
Copy link

ke6et commented Jul 16, 2013

@panarican No problem :)

$prefix = 'album_';
$fields = array(
    array( // Repeatable & Sortable Text inputs
        'label' => 'Repeatable', // 
        'desc'  => 'A description for the field.', // description
        'id'    => $prefix.'repeatable', // field id and name
        'type'  => 'repeatable', // type of field
        'sanitizer' => array( // array of sanitizers with matching kets to next array
            'featured' => 'meta_box_santitize_boolean',
            'title' => 'sanitize_text_field',
            'desc' => 'wp_kses_data'
        ),
        'repeatable_fields' => array ( // array of fields to be repeated
                'title' => array(
                'label' => 'Title',
                'id' => 'title',
                'type' => 'text'
            ),
                'featuring' => array(
                'label' => 'featuring',
                'id' => 'featuring',
                'type' => 'text'
            ),
                'producer' => array(
                'label' => 'producer',
                'id' => 'producer',
                'type' => 'text'
            ),
                'mp3' => array(
                'label' => 'mp3',
                'id' => 'mp3',
                'type' => 'text'
            )
        )
    )
);
$album_details = new custom_add_meta_box( 'album_details', 'Album Details', $fields, 'music', true );

@panarican
Copy link

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
Copy link

ke6et commented Jul 17, 2013

@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
Copy link

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
Copy link

You're Welcome 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants