1

I am using wp alchemy to add a custom metabox to my site with multiple image uploader option. This part is working fine, but I have no Idea how to go about outputting the image links to my template. I am hoping to output as a unordered list.

setup.php file

<?php

include_once WP_CONTENT_DIR . '/wpalchemy/MetaBox.php';
include_once WP_CONTENT_DIR . '/wpalchemy/MediaAccess.php';

// include css to help style our custom meta boxes
add_action( 'init', 'my_metabox_styles' );

function my_metabox_styles()
{
    if ( is_admin() )
    {
        wp_enqueue_style( 'wpalchemy-metabox', get_stylesheet_directory_uri() . '/metaboxes/meta.css' );
    }
}

$wpalchemy_media_access = new WPAlchemy_MediaAccess();

custom-spec.php

<?php
$custom_mb = new WPAlchemy_MetaBox(array
(
    'id' => '_custom_meta',
    'title' => 'Add images to home page slider',
    'template' => get_stylesheet_directory() . '/metaboxes/custom-meta.php',
    'include_template' => 'home.php'
));
?>

custom-meta.php

<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control">

    <p><a href="#" class="dodelete-docs button">Remove All</a></p>

    <?php while($mb->have_fields_and_multi('docs')): ?>
    <?php $mb->the_group_open(); ?>

        <a href="#" class="dodelete button">Remove</a>

        <?php $mb->the_field('imgurl'); ?>
        <?php $wpalchemy_media_access->setGroupName('img-n'. $mb->get_the_index())->setInsertButtonLabel('Insert'); ?>

        <p>
            <?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
            <?php echo $wpalchemy_media_access->getButton(); ?>
        </p>

        <?php $mb->the_field('title'); ?>
        <label for="<?php $mb->the_name(); ?>">Title</label>
        <p><input type="text" id="<?php $mb->the_name(); ?>" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

    <?php $mb->the_group_close(); ?>
    <?php endwhile; ?>

    <p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-docs button">Add</a></p>

</div>

functions.php file

include_once 'metaboxes/setup.php';

include_once 'metaboxes/custom-spec.php';
1
  • Did you manage to make it work ? Can you guive us the solution : ) thanks yann
    – user14680
    Mar 29, 2012 at 11:02

2 Answers 2

2

Do you have any output code we can reference?

Put this in your Theme file:

     <img src="<?php get_the_value('imgurl'); ?>” style=”width:100px;height:100px” />

if you have multiple you'll have to go through a foreach loop:

<?php

foreach ($meta['imgurl'] as $img )
{
    echo '<a href="#" /><img src="'. $img .'" /></a>';
}
?>

Hope this helps, I don't really know this Metabox class, however I've used many and it seems like it would work this way.

Good luck!

5
  • This doesn't seem to work. What output code are you looking for, I'm not sure what to post. Mar 14, 2012 at 19:26
  • If you change the $meta['imgurl'] to $meta ['img-n'] do you get a different result when wrapped in your function?
    – David
    Mar 14, 2012 at 19:30
  • no luck, do you mean change the $meta['imgurl'] in the loop not the function. Mar 14, 2012 at 19:46
  • try: var_dump($custom_mb->the_value('imgurl'); & var_dump($custom_mb->the_value('img-n'); and check your Post Meta to see if it's outputting the URL inside the meta data of the post.
    – David
    Mar 14, 2012 at 20:23
  • Im sorry what you said doesn't make any sense to me, do I just place this code in my template on its own, how Do I check my Post Meta? Do you recommend a better way to create a upload button in a custom metabox Mar 15, 2012 at 18:47
0

Here is the code I'm using:

<?php
        global $custom_mb;

        // instead of using helper functions, you can also use ...
        $sch = get_post_meta(get_the_ID(), $custom_mb->get_the_id(), TRUE);

        foreach ($sch['docs'] as $img)
        {
            echo $img['imgurl'];
        }   
?>

Almost worked fine.

2
  • 1
    What do you mean by "Almost work fine"? Nov 27, 2014 at 15:56
  • Please explain why this answer is helpful.
    – kaiser
    Nov 27, 2014 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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