WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type: meta box for adding an image? (3 posts)

  1. designutensil
    Member
    Posted 1 year ago #

    Hey all, I'm learning custom post types, custom taxonomies, and custom meta boxes at the moment. Getting the hang of things but I hit a roadblock and have been googling for hours with no luck.

    Right now I'm learning how to make meta boxes. I want the user to be able to add a product image. I know WP now has this new media uploader, but I want to be able to make things as simple, straight-forward, and restricted (fool-proof) as possible. Furthermore, I'll need to take that image they upload it, and display it in the relevant [name]-single.php. So I need the hook. How do I get this?

    Sorry for what seems like a very simple question. If anyone could even just point me in the right direction for a tut or something, that'd be wonderful. Thanks for your time!

  2. catacaustic
    Member
    Posted 1 year ago #

    The way that I've done this before is probably a bit more complicated then you're looking for, but it's a good example.

    I've set up the meta box to display a grid of all of the images that are attached to that post. When a user clicks on one of the iamges I use JavaScript to set it as selected and copy the ID of that attachment into a hidden form field. When the update form is submitted I record the ID that's in that field with update_post_meta(). Then in my template I can easily use get_post_meta() to get the ID for the image and do whatever I need to with it.

    That's just my own way of doing it for the jobs that I've done in the past. There's a lot more ways to get around it, so I'd also be happy for anyone else to give a hand with an alternate idea.

  3. designutensil
    Member
    Posted 1 year ago #

    Hmm... sounds interesting. I eventually want to build custom post-types and backends meant for clients, and of course you gotta child-proof the whole thing so they don't get lost or break things. So To be able to have like a single meta box for "upload product image" and there's the button for uploading and it shows the thumb, that would be perfect.

    Do you know of any tuts or resources I can look at for this? I'm still not "fluent" with WP and am very much still copy+pasting snippets and editing them to my needs as I become familiar with the plethora of classes and functions and the syntax for everything! (I would dig through the codex more but tbh it really only presents info about each element and not much about how to use them together in the way that I want to)

Topic Closed

This topic has been closed to new replies.

About this Topic