I’m trying to create an array of images for a slideshow gallery script, so need to separate the images from the post loop – any ideas of the best way to achieve this and make it easy to add images?
I can use a script to grab attachments (see below) but it seems a bit messy as:
- image uploaded appear in the post text area and then meed to be deleted otherwise there is duplication.
- you can’t delete and image except by going into the media library and deleting it there.
http://digwp.com/2009/08/awesome-image-attachment-recipes-for-wordpress/
Would the better option be to use something like the more fields plug-in and create multiple text fields to post the urls of photos in?
So they use the media library to upload images and grab the urls to put in the post.
Ideally I’m trying to get a simple scenario where they just upload the images in one go as a gallery or individual images, write their gallery description text, and save.
The best way to assign images to a given post is to just upload them in the WordPress post editing/new post area. You can also delete images from there.
With that said, here would be how you do that. You’re going to hook into
wp_enqueue_script
, and callwp_enqueue_script
to add your gallery script to the page. Then you’ll usewp_localize_script
, which will print out a nice javascript object for you right above your script.Inside your hooked function, you access to the
$post
variable, which contains the current post. You don’t need to be inside the loop to get that. And if you have that, you can get child posts (like attachments), which makes getting images outside the loop very easy.You can add anything you need to that JS object. Want the title attribute saved for the image?
all your upload images are saved under Media Library.You can write Image Description text at the time of a new post.
Steps:
posts->addnew->upload image->Choose media Library->select Image->Now you have more options like image(Title,Alternate Text,Caption,Description,Link URL,Alignment,Size)->Then Click Insert into post->Save All changes.
I hope this is useful for you.:)
Awful hack, but it’ll work:
As I said, awful.
You’d be better, if you’d simply but the JSON at the end of the page, for example, into the footer, but gather the images in the loop into a string still stands.
In your functions file place this:
then call the images that are uploaded to whatever post with this:
you can see that there are different options in there to set, like the image size (the first option and a few others to play with.