• 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'];

    https://www.ads-software.com/extend/plugins/option-tree/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Flaunt Books

    (@skooks)

    Error on above code… Use this:

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

    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.

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

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

    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

    Thread Starter Flaunt Books

    (@skooks)

    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?

    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 ??

    Thread Starter Flaunt Books

    (@skooks)

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

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

    Thread Starter Flaunt Books

    (@skooks)

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

    Thread Starter Flaunt Books

    (@skooks)

    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.

    white04004

    (@white04004)

    Hi,

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

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Plugin: OptionTree] Unserialize Meta Box Data’ is closed to new replies.