WordPress.org

Ready to get started?Download WordPress

Forums

OptionTree
[resolved] Unserialize Meta Box Data (12 posts)

  1. Flaunt Books
    Member
    Posted 1 year ago #

    If you use the example in Option Tree for the custom meta box for posts or pages you'll need to use the following to get it to show on both posts and pages:

    'pages'       => array( 'post', 'page' ),

    When you retrieve the custom option within the post Loop you'll most likely need to unserialize the data if it's an option type that is stored as an array.

    $custom_meta = get_post_custom($post->ID);
        $background = unserialize($custom['background'][0]);

    You can then use the data in the associative array such as the following:

    echo $background['background-image'];

    http://wordpress.org/extend/plugins/option-tree/

  2. Flaunt Books
    Member
    Posted 1 year ago #

    Error on above code... Use this:

    $custom_meta = get_post_custom($post->ID);
        $background = unserialize($custom_meta['background'][0]);
  3. Esteban
    Member
    Posted 1 year ago #

    Works.

    But i think you can just call something like this:

    $background = get_post_meta($post->ID, 'background', true);

    Using get_post_meta insted of get_post_custom.
    Being "background" the ID you put into you metaboxex.php for that particular option.
    And you will have the same data.
    Can be unserialized if you want too, or treated like whatever you want.

  4. vabmedia
    Member
    Posted 1 year ago #

    oh, thank you, would this work for images? or would I need the wp_get_attachment_image_src()? code

  5. vabmedia
    Member
    Posted 1 year ago #

    Is that code above mainly for the list-item option type or does it work on others? not in list-items?

  6. vabmedia
    Member
    Posted 1 year ago #

    Ok I have the following:

    function custom_meta_boxes() {
     $my_meta_box = array(
        'id'        => 'my_meta_box',
        'title'     => 'Images on Right Column',
        'desc'      => '',
        'pages'     => array( 'page' ),
        'context'   => 'normal',
        'priority'  => 'high',
        'fields'    => array(
          array(
            'id'          => 'firstimg',
            'label'       => 'First img',
            'desc'        => 'Add images here to appear on right column',
            'std'         => '',
            'type'        => 'upload',
            'class'       => 'fimg'
            ),
              array(
                'label'       => 'second Upload',
                'id'          => 'imgs_sec',
                'type'        => 'upload',
                'desc'        => 'Second Right Col Image',
                'class'       => 'simg'
              ),
    
          array(
                'label'       => 'Third Upload',
                'id'          => 'imgs_th',
                'type'        => 'upload',
                'desc'        => 'Third Right Col Image',
                'class'       => 'timg'
              ),
    
      	)
      );
    
      ot_register_meta_box( $my_meta_box );

    and I'm hoping to call back the images I uploaded, so I edited your code and did this:

    <?php //images
    
    $custom_meta = get_post_custom($post->ID);
        $background = unserialize($custom_meta['my_meta_box'][0]);
        $backimg = $background['firstimg'];
        echo wp_get_attachment_image_src($backimg , "full");
    
                   ?>

    And I have no luck at getting any images. :-(
    I'm sort of confused about which variables go where, as you may see in my code above

  7. Flaunt Books
    Member
    Posted 1 year ago #

    I'm not sure but I'll try to test your code. I assume you are trying to get the URL of the full size of the uploaded image?

  8. vabmedia
    Member
    Posted 1 year ago #

    Well, I'm trying to get the image to show up, period. So I guess I left out the img src code.
    Can you please try my code? It's driving me crazy :-(

  9. Flaunt Books
    Member
    Posted 1 year ago #

    Try this in place of what you have:
    $background = unserialize($custom['firstimg']);

    Then just echo $background to see if it's got data.

  10. Flaunt Books
    Member
    Posted 1 year ago #

    My example was for a background. You might need
    $background = unserialize($custom['firstimg'][0]);

  11. Flaunt Books
    Member
    Posted 1 year ago #

    You can also use print_r($background) to see the array and then use the appropriate field such as ['background-image'] or whatever...

    Try that and let me know.... Should work.

  12. white04004
    Member
    Posted 1 year ago #

    Hi,

    I'd like to know the reason to use unserialize function in my code.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic