سؤال

I need a way to create a custom field that will let me pick an image from the media gallery. How do I do that?

The custom field should have a button that when clicked will take me to the media gallery and place the src destination url within a input text box.

looking for a plugin or online tutorial and I'm having little luck.

هل كانت مفيدة؟

المحلول

A few weeks ago I added a feature similar to Magic Fields.

Here's the github project. The most important thing is in this file: js/custom_fields/media_image.js

نصائح أخرى

By using one of the plugins that already do this:

http://wordpress.org/extend/plugins/custom-field-template/

This to me sounds like the post_thumbnail feature in WordPress. It will add a box on the right hand side, below the tag box. By default the box displays a link "Set featured image".

When you click on it the media gallery popup opens and you can pick a picture you have previously uploaded to the post as the featured picture.

You can then use:

if (has_post_thumbnail()) {
  the_post_thumbnail();
}

in you theme to display the featured picture.

To enable post_thumbnail you just need to add:

add_theme_support( 'post-thumbnails' );

to your theme's function.php

To answer your question a bit sideways, are you open to an alternative? I suggest adding a photo to a post, filing the post under a specific category for your images, and maybe, depending on your needs, adding a text-only custom field to retrieve that specific image.

$image = get_posts('cat=the_category&meta_key=the_key&meta_value=the_value');
foreach($image as $img){
    setup_postdata($img);
    //whatever your markup is...
    echo '<p>'.the_content().'</p>';
    //or
   echo '<p>'.$img->post_content.'</p>';
}

Just a thought...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top